I have defined the following two scopes and both working fine:
scope :regular_friends, lambda { |user| joins{friendships}.where{friendships.user_id.not_in(user)}}
scope :inverse_friends, lambda { |user| joins{inverse_friendships}.where{inverse_friendships.friend_id.not_in(user)}}
I using squeel (https://github.com/ernie/squeel) here and building a self referential association, which is inspired by (http://railscasts.com/episodes/163-self-referential-association).
Based on that, I want to define a third scope which unions the results of the other two:
scope :friends, lambda { |user| User.regular_friends(user).union(User.inverse_friends(user)) }
Unfortunately, it doesn’t work. I am getting a:
NoMethodError (undefined method `default_scoped?' for #<Arel::Nodes::Union:0x8249534>):
I am on Rails 3.1.3. I thought the default_scope? method has been removed in Rails 3.*, but it’s complaining about that…
Can anyone help?
There is no such method as
unionin Rails (I don’t know about Arel). Just construct the scope as you would normally.EDIT:
You can always use raw sql in the scope definition. I don’t know the details of your application (especially how the tables are interconnected), so I can’t construct the query in its full beauty. Nevertheless, the query will look similar to this: