Rails: join query on association with class_name

Viewed 1018
class A < ActiveRecord::Base
  has_one :b, class_name: "Something::B"
end

module Something
  class B < ActiveRecord::Base
  end
end

Assuming above class structure with actual table names a and something_b, I want to create the following SQL query.

SELECT  "a".* FROM "a" 
INNER JOIN "something_b" ON
"something_b"."a_id" = "a"."id" 
WHERE "something_b"."some_column" = "some_value" LIMIT 1

I tried something along the lines of

A.joins(:b).find_by(b: { some_column: 'some_value' })

but the resulting query is as follows, which has "b" instead of "something_b" in the WHERE clause.

SELECT  "a".* FROM "a" 
INNER JOIN "something_b" ON
"something_b"."a_id" = "a"."id" 
WHERE "b"."some_column" = "some_value" LIMIT 1

Is there a way to do it without explicitly specifying the table name as follows?

A.joins(:b).find_by(something_b: { some_column: 'some_value' })
2 Answers
Related