Simple has_many :through question

Ok, I have 2 very simple has_many :through associations I need to
create and, despite bashing my brain and my computer against this
problem repeatedly for 2 days, I cannot figure out what I am doing
wrong as I cannot get the has_many :through to work. I think I have
fundamentally misunderstood how to do has_many :throughs, so I am
going to ruby script/destroy the models I have created and try again.
I’m going to post here my steps to create these relationship, can
someone please let me know if I’m doing this correctly? First, the
relationships.

Contacts <== Account_Contacts ==> Account <== Account_Attachments ==>
Attachments

the 2 word tables are the join tables, basically Contacts and
Attachments have a many to many relationship with Account.

  1. ruby script/generate model Account
  2. ruby script/generate model Contact
  3. ruby script/generate model Attachment
  4. fill in migration file for the account table (no need to reference
    a join table here)
  5. fill in migration file for the contact table (here I am creating
    the Account_Contacts table with integer fields for Account and
    Contact, as well as a category field)
  6. fill in migration file for the attachment table (Same as the
    contact table, I create Account_Attachments here with the same
    fields).
  7. rake db:migrate

Ok, now I should have a functioning has_many :through relationship,
right? When I attempted to use this relationship in the console like
so:

acct = Account.find(1)
file = Attachment.find(1)
acct.attachments.create( :attachment => file, :category => “test”)

I get the following error message:

NameError: uninitialized constant Account::AccountAttachment
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activesupport-1.4.2/lib
/active_support/dependencies.rb:477:in const_missing' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib /active_record/base.rb:1360:incompute_type’
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib
/active_record/reflection.rb:125:in send' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib /active_record/reflection.rb:125:inklass’
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib
/active_record/reflection.rb:177:in source_reflection' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib /active_record/reflection.rb:177:incollect’
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib
/active_record/reflection.rb:177:in source_reflection' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib /active_record/reflection.rb:186:incheck_validity!’
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib
/active_record/associations/has_many_through_association.rb:6:in
initialize' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib /active_record/associations.rb:934:innew’
from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.3/lib
/active_record/associations.rb:934:in `attachments’
from (irb):3

What did I do wrong here?

On Dec 19, 2007, at 12:57 PM, JonathanB wrote:

Ok, now I should have a functioning has_many :through relationship,
right? When I attempted to use this relationship in the console like
so:

You need to edit the models and add something like

contacts.rb
has_many :account_contacts
has_many :accounts, :through => :account_contacts

account.rb
has_many :account_contacts
has_many :contacts, :though => :account_contacts
has_many :account_attachments
has_many :attachments, :through => :account_attachments

Peace,
Phillip

On Dec 19, 2007, at 2:05 PM, gemblon (t.b.) wrote:

Phillip has a really NICE Short version.
here is a longer version, if you are interested.


Not just longer ;), but actually correct. I forgot the join table
models that have the :belongs_to. I don’t think it would have worked
without that!

Thanks gemblon!

Peace,
Phillip

Phillip K. wrote:

On Dec 19, 2007, at 12:57 PM, JonathanB wrote:

contacts.rb
has_many :account_contacts
has_many :accounts, :through => :account_contacts

account.rb
has_many :account_contacts
has_many :contacts, :though => :account_contacts
has_many :account_attachments
has_many :attachments, :through => :account_attachments

Peace,
Phillip

Phillip has a really NICE Short version.
here is a longer version, if you are interested.


OLD WAY - has_and_belongs_to_many :my_model

This is an out of date technique to set up the many-to-many table. A 

major limitation to this method is that you cannot have any more fields
in the table then the 2 joining fields, and the join table does not use
a primary field key called ‘id’. You can’t sort joined fields very
easily with this method.

This technique is very easy to set up. The table name is the plural 

names of the 2 joining tables, in alphabetical order, separated by an
underscore. Table C and B would be BS_CS. The only 2 fields in the join
table would be b_id and c_id.

NEW WAY - has_many :through

This technique allows the join table to have as many fields as you 

want. You need a model, for the joining, to make this work. You can just
generate a model. But if you need more fields, you can scaffold. The
join table also gets the primary key field (’id’) that is automatically
generated in most cases.

The joining table can be named what ever you want. The current trend 

is to use a table name that just has one word, and usually ends in
-ship, -tion, or -ment. There can be endless hours of debate over the
name of the joining table.

You will need to have the 2 joining table’s foreign key fields in 

this table, like the fashion ‘_id’, that are not pluralized.

If you wanted to join table A and table B, an example of the joining 

table could be:

TABLES

    A

        id (integer, primary key)
        name (string)

    B

        id (integer, primary key)
        name (string)

    AB (many table for joining tables A and B)

        id (integer, primary key)
        a_id (integer)
        b_id (integer)
        … (more fields)

The MODELS

    A

        has_many :ABs
        has_many :Bs :through => :ABs

    AB

        belongs_to :A
        belongs_to :B

    B

        has_many :ABs
        has_many :As :through => :ABs

SOME OPTIONS

    has_many :mytable,     :foreign_key => 'myfield_id',
                           :class_name => 'myclassname',
                           :dependent => :destroy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs