Reputation: 911
I have a common set of models extracted into a gem with namespaced models as such:
module Gemifive
class Activity < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :activities
end
end
That's in the gem "gemifive". So far, so good.
In an app where I use this gem's models, I can do the following: Gemifive::Activity.where(user_id: user.id)
. This works fine because that table has a user_id
column:
SELECT "gemifive_activities".* FROM "gemifive_activities" WHERE "gemifive_activities"."user_id" = 18`
However, the following does not work work: Gemifive::Activity.where(user: user)
. This generates the following SQL, which is invalid:
SELECT "gemifive_activities".* FROM "gemifive_activities" WHERE "gemifive_activities"."user" = 18
I can access Gemifive::Activity.first.user
just fine, so I know the belongs_to
association is working. Why can't I use this ActiveRecord convention?
Upvotes: 1
Views: 339
Reputation: 35370
Gemifive::Activity.where(user: user)
This is invalid ARel, plain and simple. It has nothing to do with your models being namespaced. In the above code, ARel is using the key of the Hash as the column name, and it will be used verbatim. What you can do is
Gemifive::Activity.where(user_id: user)
Upvotes: 3