If I have the following named_scope
named_scope :scope_by_user_id, lambda {|user_id| {:conditions => ["comments.user_id = ?", user_id]}}
is there a way in rails to only apply that condition if user_id is not nil?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure. You can put just about anything in a lambda that you would in any other Ruby block, so just modify it to return the
:conditionshash only when user_id isn’t nil. Here I’ve used a simple ternary conditional: