I have a simple scoping question. I would like to do this as a scope:
if article.responses.blank?
return false
elsif article.responses.last.passed.eql?(false)
return true
else
return false
end
So on the article model I would have something like this:
scope :failed_response, {
:joins=>[:responses],
:conditions=>["responses.passed = ?", false]
}
The problem is, I only want instances where the most recent response failed. I'm sure these is a way to do this with either fancy sorting or some kind of nested query, but I'm stuck. Thanks!
The only thing I can think of at the moment is a subquery inside the scope:
I guess there is some kind of Rails way that is a bit nicer or a way without a subquery, but I can't think of it at the moment. Hope this helps though. :)
I'd say to make it clear than clever. Have a instance method to return the last_reponse for that individual article and then have another instance method to return boolean of whether that's true or false. It may not be as fast as a name scope with single line of SQL. But I still do it the clear way for better maintainability/understanding.