Last Updated: February 25, 2016
·
2.359K
· darkside73

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

2 Responses
Add your response

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 ·