In Rails ActiveRecord, joins does not work with has_and_belongs_to_many in namespaced models -
i have 2 models in namespace, service , instructor, both have many many relationship each other, defined through 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
in scope of_service, want of_service return instructors associated service.
however, when running scope, error:
activerecord::statementinvalid: pg::error: error: missing from-clause entry table "services" line 1: ...."id" = "instructors_services"."service_id" "services"... ^ : select "scheduling_instructors".* "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" "services"."id" = 107 limit 1
where seems going wrong joining table called instructors_services (that table doesnt exist), ignoring namespace in related model. should joining table called scheduling_instructors_services, consistent namespace.
try adding :join_table => 'scheduling_instructors_services'
relations this:
has_and_belongs_to_many :instructors, :class_name => "scheduling::instructor", :join_table => 'scheduling_instructors_services' has_and_belongs_to_many :services, :class_name => "scheduling:: service", :join_table => 'scheduling_instructors_services'
also, shouldn't be
.where('instructors_services.id' => service_id)
instead of
.where(:services => {:id => service_id})
Comments
Post a Comment