我有一個命名空間中兩種型號,服務和教練,這兩者有許多相互一對多的關係,通過has_and_belongs_to_many定義不起作用:在Rails的ActiveRecord,加入與has_and_belongs_to_many在命名空間模型
class Scheduling::Service < ActiveRecord::Base
has_and_belongs_to_many :instructors
end
class Scheduling::Instructor < ActiveRecord::Base
attr_accessible :first_name, :last_name
has_many :instructor_availabilities
scope :of_service, lambda { |service_id| joins(:services).where(:services => {:id => service_id})}
has_and_belongs_to_many :services, :class_name => "Scheduling::Service"
end
在_service的範圍中,我希望of_service返回與服務關聯的所有教師。
但是,運行這個範圍時,我得到一個錯誤:
ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table "services" LINE 1: ...."id" = "instructors_services"."service_id" WHERE "services"... ^ : SELECT "scheduling_instructors".* FROM "scheduling_instructors" INNER JOIN "instructors_services" ON "instructors_services"."instructor_id" = "scheduling_instructors"."id" INNER JOIN "scheduling_services" ON "scheduling_services"."id" = "instructors_services"."service_id" WHERE "services"."id" = 107 LIMIT 1
它似乎是想錯了的是,其加入的表稱爲instructors_services(該表犯規存在),忽略了命名空間相關模型。它應該加入名爲scheduling_instructors_services的表,該表與名稱空間一致。
哪裏:join_table走? – 2013-05-08 14:14:56
更新回答與兩個habtm雙方的例子。 – 2013-05-08 14:25:58