# Conventions/Model

**URL:** <https://www.ruby-forum.com/t/conventions-model/133377>\
**Category:** Rails\
**Created:** [March 31, 2008, 1:44am UTC](https://www.ruby-forum.com/t/conventions-model/133377 "2008-03-31T01:44:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [March 31, 2008, 1:44am UTC](https://www.ruby-forum.com/t/conventions-model/133377/1 "2008-03-31T01:44:16Z")

</div>

All,

I’ve read about twelve different articles and I’ve yet to find a  
consistent solution to my conundrum.

I have a ‘people’ table and an associated ‘person’ model object.  
I have a ‘preferences’ table and an associated ‘preference’ model  
object.

People is as trivial as: id, first\_name, last\_name.  
Preferences - also trivial: id, name

Now I need to create a nice join table (with a ‘value’ attribute)…my  
question is: do I name it ‘people\_preferences’ or ‘person\_preferences’.  
I’m inclined to the latter. The definition is pretty obvious:  
person\_id, preference\_id, value.

Next question, assuming we go wtih person\_preferences and I generate the  
rails model: ./script/generate model person\_preference, I’m thinking my  
model associations look something like the following - can  
someone/anyone validate?

class Person  
…

has\_many :person\_preferences, :dependent =\> :destroy  
has\_many :preferences, :through =\> :person\_preferences

…  
end

class Preference  
…

has\_many :person\_preferences, :dependent =\> :destroy  
has\_many :people, :through =\> :person\_preferences

…  
end

class Person\_Preference  
…

belongs\_to :person  
belongs\_to :person

…  
end

Interacting with the model would look like:

@person.person\_preferences.each{|pref| puts “#{pref.preference.name}” :  
#{pref.value}}

Thoughts? Am I crazy? Way off the mark?? TIA for any help!

---

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [March 31, 2008, 2:10am UTC](https://www.ruby-forum.com/t/conventions-model/133377/2 "2008-03-31T02:10:09Z")

</div>

Hi Cory,

You have data on the “join table” therefore you’re better using  
has\_many :through, because without making the model a full AR model,  
you won’t be able to access the data on it.

So, this is what I’d do:

table name is people  
class Person \< ActiveRecord::Base  
has\_many :person\_preferences  
has\_many :preferences, :through =\> :person\_preferences  
end

table name is person\_preferences, has attributes person\_id,  
preference\_id  
class PersonPreference \< ActiveRecord::Base  
belongs\_to :person  
belongs\_to :preference  
end

table name is preferences  
class Preference \< ActiveRecord::Base  
has\_many :person\_preferences  
has\_many :people, :through =\> :person\_preferences  
end

This gives you maximum flexibility. Note, however, that the hm=\>thr  
associations act a little differently than simple has\_many or  
has\_and\_belongs\_to\_many associations . I’d look up the docs for both  
if I were you in ActiveRecord::Base in the API, just to referesh your  
mind.

If you were going to use a has\_and\_belongs\_to\_many with a join table,  
I’d name the join table what rails expects, which is alphabetically  
ascending ordered plural names. Ie:

cats and dogs would end up being cats\_dogs (cats comes before dogs  
alphabetically, both are plural)

people and preferences would end up being people\_preferences (people  
comes before preferences alphabetically and both are plural).

Julian.
