Note that there are some explanatory texts on larger screens.

plurals
  1. PORails form with three models and namespace
    primarykey
    data
    text
    <p>Banging my head against this one for a long time. On Rails 2.3.2, Ruby 1.9.1.</p> <p>Trying to use one form to create three objects that have these relations:</p> <pre><code>class Person has_one :goat end class Goat belongs_to :person has_many :kids end class Goat::Kid belongs_to :goat end </code></pre> <p>Here's a summary of the schema:</p> <pre><code>Person first_name last_name Goat name color Goat::Kid nickname age </code></pre> <p>I'd like my <code>#create</code> action to instantiate new instances of all three models with the specified associations. However, while it appears that my params hash is being passed to the controller as it should (based on the backtrace logs in the browser when it blows up), the <code>Goat::Kid</code> object is not collecting the params.</p> <p>irb (irb session is just a psuedo-representation of what I'm trying to accomplish so if it doesn't call <code>#save!</code> or any other necessities it's not really meant to be correct. I'm trying to do this all through the browser/web form.)</p> <pre><code>a = Person.new :first_name =&gt; 'Leopold', :last_name =&gt; 'Bloom' b = Goat.new :name =&gt; 'Billy', :color =&gt; 'white' c = Goat::Kid.new :nickname =&gt; 'Jr.', :age =&gt; 2 a.goat.kids &gt;&gt; [] </code></pre> <p>Now, I cannot figure out how to get the view to pass the params to each object and to get the controller to save these params to the db.</p> <p>My questions: A) is this a good place to use <code>nested_attributes_for</code> and if so how do I declare that with a namespace? B) is there a much simpler, easier to understand way to do this?</p> <p>Passing params to three models has just been very challenging to me and no matter how much documentation I read I can't wrap my head around it (<code>#form_for</code> and <code>#fields_for</code>). The namespace further complexifies this. Thanks for any help!</p> <hr> <p>Addendum: if I end up declaring</p> <pre><code>accepts_nested_attributes_for </code></pre> <p>what's the proper way to use the symbol argument for a namespaced model?</p> <pre><code>accepts_nested_attributes_for :kids, :through =&gt; :goats </code></pre> <p>or</p> <pre><code>accepts_nested_attributes_for :goats_kids, :through =&gt; :goats </code></pre> <p>or</p> <pre><code>accepts_nested_attributes_for :goats::kids, :through =&gt; :goats </code></pre> <p>I'm not sure how namespaced models translate to their symbol identifiers. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload