Issue with mering ActiveRecord query results

14 views
Skip to first unread message

Sumit Srivastava

unread,
Aug 7, 2013, 4:34:49 AM8/7/13
to rubyonra...@googlegroups.com
class ABC

def self.active

    self.live.where('starts_at <= ?', Time.now).where('(ends_at > ? || ends_at IS NULL)', Time.now)

  end

  def self.recently_completed`

    self.completed.where("completed_at > ?", 2.days.ago)`

 end

The issue is when I do:

(ABC.active + ABC.recently_completed).columns_hash

This gives error, undefined method `columns_hash' for #<Array:.......

When I do:

(ABC.active).merge(ABC.recently_completed)

The query being run is actually combination of the two. They are not run independently as I expected.

I need a way to write the code as to run the queries separately and then merge the results but not as an array.


Joel Pearson

unread,
Aug 8, 2013, 12:30:00 PM8/8/13
to rubyonra...@googlegroups.com
If you were going to use Hash#merge to remove duplicates between the two
result sets, you might as well just use Array#uniq on the Array.

--
Posted via http://www.ruby-forum.com/.

Dheeraj Kumar

unread,
Aug 8, 2013, 12:49:08 PM8/8/13
to rubyonra...@googlegroups.com
@Joel:

It isn't Hash#merge, but ActiveRecord::SpawnMethods#merge

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages