Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Rails ActiveRecord, joins does not work with has_and_belongs_to_many in namespaced models
    primarykey
    data
    text
    <p>I have two models in a namespace, a service and an instructor, both which have a many to many relationship with each other, defined through a has_and_belongs_to_many:</p> <pre><code>class Scheduling::Service &lt; ActiveRecord::Base has_and_belongs_to_many :instructors end class Scheduling::Instructor &lt; ActiveRecord::Base attr_accessible :first_name, :last_name has_many :instructor_availabilities scope :of_service, lambda { |service_id| joins(:services).where(:services =&gt; {:id =&gt; service_id})} has_and_belongs_to_many :services, :class_name =&gt; "Scheduling::Service" end </code></pre> <p>In the scope of_service, I want of_service to return all instructors that are associated with a service.</p> <p>However, when running this scope, I get an error:</p> <blockquote> <p>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</p> </blockquote> <p>Where it seems to be going wrong is that its joining a table called instructors_services (that table doesnt exist), ignoring the namespace in the related model. It should be joining a table called scheduling_instructors_services, which is consistent with the namespace.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload