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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/62576/375b7e3e6b2365fdd0f127f4e224f4dc.jpeg)
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
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/72074/5b986d0035c3c1f40acc3e59efd6a65c.jpeg)
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#