First or limit in Rails scope
ruby-on-rails, ruby-on-rails-3
Solution
Just stick with your original idea:
scope :user_reviews, lambda { |user| where(:user_id => user) }
and call `user_reviews.first`. Nothing wrong with that.
Definitely do not define a scope that returns a single object. A scope should be chainable.
Problem
I have the following scope: ``` scope :user_reviews, lambda { |user| where(:user_id => user) } ``` I apply this in the controller: ``` def show @review = @reviewable.reviews.user_reviews(current_user).first || Review.new end ``` The `first` is to limit to search the current user's one and only review. Now I try to write a new scope `user_review` which I tried many ways to chain the `user_reviews` scope with `first`, but just couldn't get it what. Something like this: ``` scope :user_reviews, lambda { |user| where(:user_id => user) } scope :user_review, lambda { |user| user_reviews(user).first } ``` I know the above `user_review` is wrong, but just trying to show you guys what I am trying to do. How should I write this properly? Thanks.