Topic: Issue with merging AR queries

I've been trying all different ways of combining to queries but I'm not having any luck at all. It's either returning one or the other depending on what query I put first.

Here's the scopes:

  scope :for_group_with_account, lambda { |account| joins(:group => :accounts).where("accounts.id = ?", account.id) }
  scope :for_task_with_account, lambda { |account| joins(:tasks => :account).where("accounts.id = ?", account.id) }
  scope :for_account, lambda { |account| for_task_with_account(account).merge(for_group_with_account(account)) }

For the last scope where I combine the scopes I've also tried:

  scope :for_account, lambda { |account| for_task_with_account(account) + for_group_with_account(account) }
  scope :for_account, lambda { |account| for_task_with_account(account) & for_group_with_account(account) }
  scope :for_account, lambda { |account| for_task_with_account(account) && for_group_with_account(account) }

Still none of these are actually taking the two listings and combining them. Very very frustrating. Is there something I'm doing wrong?

Thanks.

Re: Issue with merging AR queries

Try this:

  scope :for_account, 
    lambda { |account|
       for_task_with_account(account).concat(for_group_with_account(account)) 
    }
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Issue with merging AR queries

Ok that works. But now I'm getting...

undefined method `search' for #<Array:0x107a44428>

The search method is the meta_search gem. Same things actually happens if I use the paginate method too. Except it'll say...

undefined method `paginate' for #<Array:0x107a44428>

It's not liking that it's returning an array.

Re: Issue with merging AR queries

Yea I was afraid of that,  Is there any reason you have to combine two scopes like that,  can you define a third stand alone combo scope and just use an SQL UNION to achieved the combining?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Issue with merging AR queries

Ok How would I go about writing that up? Thanks!

Re: Issue with merging AR queries

I've tried this...

scope :for_account, lambda { |account| joins(:tasks => :account, :group => :accounts).where("accounts.id = ?", account.id) }

But it doesn't return the right stuff either.

Re: Issue with merging AR queries

Well, the first thing would be to just craft the SQL manually that gets the results you want.

scope :for_account , find_by_sql("SELECT * FROM account where ......  UNION SELECT * FROM account WHERE ......")

But before that,  try something like this,  I haven't tested, nor thought it through complete

scope :for_account,  find_by_sql("#{Account.for_group_with_account.to_sql} UNION #{Account.for_task_with_account.to_sql}")

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Issue with merging AR queries

Ok I had to modify it a bit to match what I'm doing:

scope :for_account, lambda { |account| find_by_sql("#{for_group_with_account(account).to_sql} UNION #{for_tasks_with_account(account).to_sql}") }

But the problem is, is that it's returning an array class rather than a ActiveRecord::Relation class. So when I try to append another method or scope after calling this scope I get the error:

undefined method `not_archived' for #<Array:0x10b3e0600>

or

undefined method `search' for #<Array:0x10b3e0600>

I'm using meta_search for example. Anything else I can do?

Thanks!

Re: Issue with merging AR queries

I haven't tested, nor thought it through complete .http://www.bosin.info/g.gif