# Model Composition in Rails

**URL:** <https://www.ruby-forum.com/t/model-composition-in-rails/51337>\
**Category:** Rails\
**Created:** [December 29, 2005, 9:55pm UTC](https://www.ruby-forum.com/t/model-composition-in-rails/51337 "2005-12-29T21:55:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradeck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bradeck](https://www.ruby-forum.com/u/bradeck)\
**Post date:** [December 29, 2005, 9:55pm UTC](https://www.ruby-forum.com/t/model-composition-in-rails/51337/1 "2005-12-29T21:55:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bradeck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bradeck](https://www.ruby-forum.com/u/bradeck)\
**Post date:** [December 30, 2005, 12:14am UTC](https://www.ruby-forum.com/t/model-composition-in-rails/51337/2 "2005-12-30T00:14:06Z")

</div>

On 12/29/05, Eck, Brad [removed\_email\_address@domain.invalid](mailto: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”
