2

I have some scopes definied in a Model called Events. Now I need join them using OR operator. I’m using Rails3 and I’m a bit confused with ActiveRelation & Arel and how and how I should use this

scope :issues,   where(:subject_type =>  "Issue")
scope :for_company, lambda {|company| joins(:user)
                                      .where(:users => { :company_id => company.id })
                                      .includes(:user)}

How I can get events

for_company(x) OR issues

When I try Events.for_company(X).or(issues) I get an error

Events.for_company(Company.find(1)).or(Events.issues)

NoMethodError: undefined method `or' for #ActiveRecord::Relation

using Events.for_company(Company.find(1)).arel.or(Events.issues) I get

NoMethodError: undefined method `or' for #Arel::SelectManager

I want get all events that are issues or where user is from a determined company.

select * from events 
         join users  
           on events.user_id = users.id 
        where users.company_id = 1 
           or events.subject_type = "Issues"

Thanks.

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • Could you be more specific on what you want to get as a result. One of your scopes is doing a join so you can't just add on another query which does no joins without doing a join on it as well or omitting joined columns. – Tomas Mar 11 '11 at 10:54
  • First of all you in second scope you have joins and includes for users. It's not correct, just leave joins or includes (I think includes will only work with OR). There are no easy possibility to make OR for those two quiries, but you can do it using arel_table. – Dmitry Polushkin Mar 31 '11 at 20:59

2 Answers2

2

Ruby on Rails 3 howto make 'OR' condition
Rails3: combine scope with OR

Community
  • 1
  • 1
clyfe
  • 23,695
  • 8
  • 85
  • 109
-3
Event.for_company(Company.find(1)).issues
DeathHammer
  • 650
  • 6
  • 11