Rails merge scopes
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
scope :user_answers, -> { where (user_id: current_user.id) }
scope :user_answers_with_questions, -> { joins(:questions) & Answer.user_answers }
end
Written by Andrey Garbuz
Related protips
2 Responses
You should use | instead of & if you want to merge. Also I think you don't need to do Answer.useranswers, just call useranswers. Scopes are class methods and you are calling a scope from another, so you should not have any kind of problem with that.
over 1 year ago
·
Sounds reasonable. I'll try it. Thanks
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#