ruby on rails - Nested Model Forms -- How do you create an object through an association if it doesn't already exist? -


From Ryan Bates, I get the concert , band and Exhibit They play I can add performance to a new concert or when I edit a concert when I follow my tutorial, it is quite straightforward and straightforward.

display band and display belongs to related concerts . In one display, there will be a list of band playback and start / end of time. The band has only one string column for its name.

When I create a new concert and add a whole number of demonstrations, then at the same time, I band objects if they are not already present otherwise the user Has an extra step to make those objects, which will be tedious.

How does accept _nested_atends / for or some other useful rail facility, can I do this? I am using Rail 2.3.8

These are my institutions:

  class bands & lt; ActiveRecord :: Base has has_many: display has_many: concert ,: through => : End of the Performance Or Class Concert & lt; ActiveRecord :: Base has has_many: Display has_many: band ,: through: = & gt; : Accepts for display_assisted_data_production: display, reject_f = & gt; Lambda {| One | A [: content] .blank? } ,: Allow_destroy = & gt; True end square display & lt; ActiveRecord :: Base is_to: band is_to: concert end    

Rails can handle it for you You need to make sure that you pass correctly in Paramams (which should be done for you in many nested forms)

  class concert & lt; ActiveRecord :: Base has has_many: Display has_many: band ,: through: = & gt; : Accepts for performance __ nested_attributes_: display end class display & lt; Active Records :: Base official: Concert is_mind: The band accepts_nested_data_apaign: band and class band & lt; ActiveRecord :: Base is_to: performance end   

Your parameter hash should look like this:

  {: concert = & gt; ; {: Display_right = & gt; [{: Band_dates = & gt; [{: Name = & gt; "Test u"}] :: name = & gt; "Test p"}] ,: name = & gt; "Test"}}    

Comments