I'm using ruby 1.9.3
and ruby-datamapper with postgresql
. I have got a table full of entries, each of them has three properties: id
,text
and created_at
. The last one is a Date
object.
In my application I SELECT
some entries like so: collection = Entry.all(:text => /SomeRegexp/)
. I now want to know all different values of created_at
that occur in the returned DataMapper::Collection
. I thought of the following two ways:
Iterate through the
DataMapper::Collection
and collect all dates.dates = Array.new Entry.all(:text => /SomeRegexp/).each { |entry| dates.include?(entry.date) ? next : dates = dates << entry.date }
Pros:
- It should work
Cons:
- It's slow for massive amounts of data
- It should work
Ask the database for entries of each day and collect dates where collection != nil
dates = Array.new for date in DatabaseStartDate..Date.today Entry.all(:created_at => date, :text => /SomeRegexp/).empty? ? next : dates = dates << date) end
Pros:
- Should be faster than the example above, especially with big amounts of data
Cons:
- Many Database queries
- Should be faster than the example above, especially with big amounts of data
NOTICE: The code above is untested pseudo code, it should just give readers a rough idea of what the real code should do, so it's not likely to work. If you need a more detailed example feel free to comment, I will try to explain it better then!
NEXT NOTICE: If you find errors in the pseudo code please tell me, so I can get started faster! ;)
So finally my question is: Which solution should I prefer or is there a better one that didn't come into my mind?