Model Composition in Rails


#1

I’m struggling understanding the model view of a combined model. Take
the following:

class P4 < ActiveRecord::Base

  has_many :p4_priorities

end

I figured out the naming and referencing of the sub models (i.e.
P4.p4_priorities - seems simple enough but I had weird issues related to
it). I’m all ok except that I am having issues with how to reference a
‘sub-model’ in a form field. This is what I have:

        <%= text_field 'p4_p4_priorities', 'detail', :size => 75 %>

No errors - it’s just not saving which tells me the framework isn’t
understanding what to save. Can you help?

Also, the models associated with the related tables have validation but
that doesn’t apparently roll up into the combined edit I am providing.
Is that expected? If so, how do I reference those fields in validation
in this model? Just a typical dereference as mentioned above?


Brad Eck
Sr. Software Engineer
Pelco
3500 Pelco Way
Clovis, CA 93612
Office - 800-289-9100
Email - removed_email_address@domain.invalid BLOCKED::mailto:removed_email_address@domain.invalid


#2

On 12/29/05, Eck, Brad removed_email_address@domain.invalid wrote:

  has_many :p4_priorities

A text field contains a single string of text, but you’re trying to
call it on a collection of things. That’s equivalent to saying:
<%= text_field ‘all_rock_musicians_in_the_world’, ‘firstname’ %>,
which doesn’t really make sense.

Your best bet is to put this in a partial form, and call it with:
render(:partial => ‘priority’, :collection => @p4.p4_priorities)

The partial would then have text_field, etc, etc, to deal with a
single priority:
<% @priority = priority -%> (The helpers require instance variables.)
<%= text_field ‘priority’, ‘detail’, :index => priority_counter, :size
=> 75 %>

When you’re done, you’ll have a numbered array of those fields, one
per “p4_priority”