Does Mongoid split nested attributes into two hashes?

I have a List model with nested attributes from another, Article . What errors I have, I have Ruby request parameters that have my first nested attribute in the Article hash, and all the rest (2nd, 3rd article, etc.) in the article_parameters parameters.

I follow the mongoid standard tutorials here.

I have @ list.articles.build in the method of creating a List controller. Can I influence the build method? I tried calling build(all_the_correct_hashes) , but it seems the problem is not fixed.

So, in a nutshell, I have 2 questions.

  • why do articles get only the first nested attribute, the second and next go to article_parameters hash?

  • Why does list.articles.build only create _id in my Mongo document, but not fill it with other fields?

+6
source share
1 answer

I will write my decision, because I see that people are raising my question.

Honestly, I did not find the answer to why this behavior occurs. The solution is a bit hacky.

In the save controller, call List.article.build(id => params[:id], someotherstuff => params[:someotherstuff])

Everything that you pass for assembly in this case will be populated with the article_parameters hash.

id and sometherstuff are fields of my Article model.

I feel this should be done implicitly by Mongoid, but that just doesn't work. Their development does not see anything wrong with that.

0
source

All Articles