Rails nested form with has_many :through, how to edit attributes of join model? -


Acceptpts_nested_attributes_for?

How do you edit the properties of joining a model? I have 3 models: topics and articles included by linkers

  class topics & lt; ActiveRecord :: Base has has_many: linkers have has_many: articles, via = & gt; : Linkers ,: foreign_key => : Article_id accepts_ts_lights_submission: end articles of articles & lt; ActiveRecord :: Base has has_many: linkers have_many: topics ,: through = & gt; : Linkers ,: foreign_key => : Topic_id & Class Linker & lt; ActiveRecord :: Base # is included in this model, it has additional features like "relevance": subject is related to: end of article  

So when I create an article in "new" action Theme Controller ... Create a nested form in

@ topic.articles.build

... and topics / new.html .erb. .

  & lt;% form_for (@topic). Topic_form | & Gt%; ... field ... & lt;% subject_form.fields_for: article | Article_form | & Gt%; ... field ...  

... Rail automatically creates linkers, which is great For my question now: < / Strong> My Linker model also has features that I want to be able to change through the "new topic" form. But the linker, which freely trains, has zero values ​​for all the properties except for topic_id and article_id. How can I put a field in the "new topic" form for those other linker features so that they do not come out of zero?

Answered the answer. The move was:

prefix @ topic.linkers.build.build_article

This creates linkers, then creates an article for each linker. So, in the model: to accept : topic.rb: accepts_claimed_at_s_links: linkers
need linker.rb accepts_nested_atg: article

Then form:

  & lt;% = form_for (@topic). Topic_form | & Gt%; ... field ... & lt;% = topic_form.fields_for: linkers do | Linker_form | & Gt%; ... linker field ... & lt;% = linker_form.fields_for: do the article. Article_form | & Gt%; ... Text field ...  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -