lookup_by
lookup_by copied to clipboard
Support for ActiveRecord::Base.joins
With a typical belongs_to
association you can use the nice shorthand
class Foo < ActiveRecord::Base
belongs_to :bar
end
Foo.joins(:bar).to_sql
# => "SELECT \"foos\".* FROM \"foos\" INNER JOIN \"bars\" ON \"bars\".\"bar_id\" = \"foos\".\"foo_id\""
With lookup_for
, the association is never registered, and so the query has to be written out
class Foo < ActiveRecord::Base
lookup_for :bar
end
Foo.joins(:bar)
# => #<ActiveRecord::ConfigurationError: Association named 'bar' was not found on Foo; perhaps you misspelled it?>
This is related to #11 and my failed PR #18; it's another place where the only way I know of to make this happen is to hack into AR internals. Or, alternatively, pretty sure you could do this in ARel without too much of a headache.