0

I am using a scope to get an Item for a specific user:

In Item model

belongs_to :user    
scope :for_user, lambda { |user| where(:user_id => user) }

User model

has_many :items

Problem

When calling Item.includes(:user).for_user(3) an Array is returned instead of an ActiveRecord relation. I'd expect it to behave like Item.includes(:user).find_by_user_id(3), which returns a ActiveRecord relation.

Thanks for your help.

2 Answers 2

3

if you do some more investigation you'll find ou that it does indeed return a relation object.

But it will when necessary convert it into an array.

Namely, if you're in a console and say > Item.includes(:user).for_user(3) it will try to inspect it, and consequently do the conversion.

But by all means the following will work

scope = Item.includes(:user).for_user(3)

# does a db count
scope.count

# does a db select limit 1
scope.first

# does a full db select
scope.all
Sign up to request clarification or add additional context in comments.

1 Comment

ah.. when you say "ActiveRecord relation object" do you mean "an ActiveRecord" or an "ActiveRecord relation"? These are two different things.
0

Using where(:user_id => user) instead of dynamic method like find_by_user_id would always return an array. If you're only interested in the first record returned from your scope you can change your scope to be something like

scope :for_user, lambda { |user| where(:user_id => user).first }

1 Comment

This doesn't work for me. It does the following: SELECT cities.* FROM cities WHERE cities.name = 'Berlin' LIMIT 1 SELECT cities.* FROM cities. And by that end up with an array of all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.