6
votes

I've got a model List with nested attributes from another one, Article. What bugs me is I have Ruby request params having my first nested attribute in Article hash, and all the others(2nd,3rd article data, etc) in article_parameters.

I follow the standard mongoid tutorial here.

I have @list.articles.build in create method of List controller. Can I influence what goes into build method? I've tried to call build(all_the_correct_hashes) but it does not seem to fix the issue.

So, in the nutshell I have 2 questions.

  1. why articles gets only the first nested attribute, 2nd and next go to article_parameters hash?

  2. why list.articles.build only creates _id in my Mongo document, but does not populate it with other fields?

1
Hard to say what's exactly being asked here. Post relevant code (with data) - Sergio Tulentsev

1 Answers

0
votes

I'll write up my solution, since I see people upvoting my question.

Frankly, I did not find any answer as to why such behaviour occurs. The solution is a bit hackish.

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

Everything you pass to build in this case will populate article_parameters hash.

id and sometherstuff are fields of my Article model.

I feel this should be done implicitly by Mongoid, but it just does not work. Their development does not see anything wrong with it, either.