I''m trying to set up a has_many through association, and it should go through a belongs_to assocation. class Company has_many :videos end class Site belongs_to :company has_many :videos, :through => :company end Site.find(:first).videos ActiveRecord::StatementInvalid: PGError: ERROR: column companies.company_id does not exist : SELECT videos.* FROM videos INNER JOIN companies ON videos.company_id = companies.id WHERE ((companies.company_id = 1)) From the documentation: ":through: Specifies a Join Model to perform the query through. Options for :class_name and :foreign_key are ignored, as the association uses the source reflection. You can only use a :through query through a belongs_to or has_many association." So it seems as though I should be able to do it...but AR is doing the join incorrectly. Any ideas? Pat --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/17/07, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m trying to set up a has_many through association, and it should go > through a belongs_to assocation. > > class Company > has_many :videos > end > > class Site > belongs_to :company > has_many :videos, :through => :company > end > > Site.find(:first).videos > > ActiveRecord::StatementInvalid: PGError: ERROR: column > companies.company_id does not exist > : SELECT videos.* FROM videos INNER JOIN companies ON > videos.company_id = companies.id WHERE ((companies.company_id = 1)) > > From the documentation: > > ":through: Specifies a Join Model to perform the query through. > Options for :class_name and :foreign_key are ignored, as the > association uses the source reflection. You can only use a :through > query through a belongs_to or has_many association." > > So it seems as though I should be able to do it...but AR is doing the > join incorrectly. Any ideas?site.company.videos ? or... class Site belongs_to :company delegate :videos, :to => :company end Perhaps the docs are unclear? Here''s a couple has_many :through examples: class Article has_many :comments has_many :users, :through => comments end class Comment belongs_to :user belongs_to :article end class User has_many :articles has_many :comments, :through => :articles end -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m a newbie, but I''m going to take a crack at this one.>From what I understand, you have three models: Site, Company, andVideo>From your example, I don''t understand which model describes the pivottable -- the table in the middle of the many to many relationship I believe you are trying to establish, if I understand correctly. I think your pivot table is sites, for the model Site. I believe that would look like this: class Company has_many :sites has_many :videos :through => :sites end class Site belongs_to :companies belongs_to :videos end class Video has_many :sites has_many :companies :through => :sites end If you are trying to make a many to many relationship between videos and companies, then the above is how you do it. The pivot table belongs to each of the tables it joins; each table in the many-to-many relationship has_many of the other table, but each also has_many of the pivot table. Charlie On Aug 17, 3:20 pm, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to set up a has_many through association, and it should go > through a belongs_to assocation. > > class Company > has_many :videos > end > > class Site > belongs_to :company > has_many :videos, :through => :company > end > > Site.find(:first).videos > > ActiveRecord::StatementInvalid: PGError: ERROR: column > companies.company_id does not exist > : SELECT videos.* FROM videos INNER JOIN companies ON > videos.company_id = companies.id WHERE ((companies.company_id = 1)) > > From the documentation: > > ":through: Specifies a Join Model to perform the query through. > Otions for :class_name and :foreign_key are ignored, as the> association uses the source reflection. You can only use a :through > query through a belongs_to or has_many association." > > So it seems as though I should be able to do it...but AR is doing the > join incorrectly. Any ideas? > > Pat--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---