Has_many :through question

Hi,
I am having some difficulty getting has_many relationship setup
correctly.

def acts_as_markable
has_many :marks, :as => :markable
belongs_to :author, :class_name => “WebsiteUser”
end

class Video < ActiveRecord::Base
set_table_name “markables”
acts_as_markable
end

class Mark < ActiveRecord::Base
acts_as_markable
belongs_to :markable, :polymorphic => true
end

class WebsiteUser < ActiveRecord::Base
has_many :marks
has_many :videos_marked, :through => :marks, :source
=> :markable, :source_type => “Video”, :class_name =>
“Video”, :foreign_key => :author_id
end

running WebsiteUser.first.videos_marked I get this:
SQLite3::SQLException: no such column: marks.website_user_id: SELECT
“markables”.* FROM “markables” INNER JOIN “marks” ON “markables”.id =
“marks”.markable_id AND “marks”.markable_type = ‘Video’ WHERE
((“marks”.website_user_id = 1))

Correct query would be:
SELECT “markables”.* FROM “markables” INNER JOIN “marks” ON
“markables”.id = “marks”.markable_id AND “marks”.markable_type =
‘Video’ WHERE ((“marks”.author_id = 1))

any ideas? thank you.

found the problem :slight_smile:

had to change WebsiteUser’s has_many :marks to include :foreign_key =>
author_id

class WebsiteUser < ActiveRecord::Base
has_many :marks, :foreign_key => :author_id
has_many :videos_marked, :through => :marks, :source
=> :markable, :source_type => “Video”, :class_name => “Video”
end

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