我有一个命名空间中两种型号,服务和教练,这两者有许多相互一对多的关系,通过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