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.
- ruby script/generate model Account
- ruby script/generate model Contact
- ruby script/generate model Attachment
- fill in migration file for the account table (no need to reference
a join table here) - 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) - fill in migration file for the attachment table (Same as the
contact table, I create Account_Attachments here with the same
fields). - 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:in
compute_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:in
klass’
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:in
collect’
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:in
check_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:in
new’
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?