0

I currently have this scope:

class User

  has_many :inbox_messages, :through => :message_users do
    def unread
      published.where(:message_users => { :sender => false, :trash => false , :deleted => false}).where(MessageUser.arel_table[:read_at].not_eq(nil))
    end

  end
end

And it's working. But I was wondering if there is way to merge the second where into the first one.

Nerian
  • 15,901
  • 13
  • 66
  • 96

1 Answers1

3

If I understand your code correctly, you should be able to do that with

published.where(:message_users => { :sender => false, :trash => false , :deleted => false, :read_at => nil })
Stuart M
  • 11,458
  • 6
  • 45
  • 59
  • 1
    Then you DO need it as a separate `where` statement that uses something like `.arel_table[:read_at].not_eq(nil)`, since you can't mix that into the existing Hash you have passed in the `:message_users` option. – Stuart M Apr 02 '13 at 19:38
  • 1
    There is also the open source [Squeel](https://github.com/ernie/squeel) library which makes these kinds of expressions more natural with a custom DSL (e.g., you could use `!= nil`). See its docs for details. – Stuart M Apr 02 '13 at 19:39