1

I have an active relation Bar object with an attribute shift_date. shift_date represents each day between March and June. March and June comes from Foo which has attributes start_month and end_month:

f = Foo.find(1)
days = (f.end_month - f.start_month).to_i
weeks = (days * 0.142857).round(2)

f.bars will give me days objects. Where days is the total amount of objetcs.

My trouble is to get Bars objects, objects for week 1, 2 or 3 etc:

f.bars.where('shift_date >= ?', (weeks/7.days)).group_by{ |result| result }

operator does not exist: timestamp without time zone >= numeric

So what am I saying? Give me all objects on week 1 or week 5, if any. How do I go about this, please?

Im on to something but not right:

f.bars.where('shift_date >= ?', Date.today).group_by{ |result| result}

Edit:

Im almost there. I could splat out the days with:

days_array = *(f.start_month..f.end_month)

then

f.bars.where(shift_date: days_array[0]..days_array[7])

That would be the answer! But...not really. For my views, I need to group the splatted days in a 7 days interval as week, so days_array[0] to days_array[7] would be week 1 and days_array[8] to days_array[14] would be week 2 etc. How to show that in the view? This will give me everything I need.

Sylar
  • 11,422
  • 25
  • 93
  • 166
  • If by less server usage you mean you don't want to see a bunch of recursive queries then you want to create a view in the DB and let all of the work happen in the DB. If that appeals to you I can help you create a ruby object that acts like a model and accesses the view directly. – Beartech Nov 07 '16 at 20:16
  • @Beartech Well.. once all processing done on the server that's fine by me ;) – Sylar Nov 07 '16 at 20:17
  • Also can you give an example of the actual output you are looking for? That is, can you show me what you wanted `f.bars.where('shift_date.....` to return? – Beartech Nov 07 '16 at 20:17
  • @Beartech Ive made an edit, see last code. I need to replace `Date.today` with the week I need. – Sylar Nov 07 '16 at 20:18
  • Ok, I need you to give and example of the OUPUT data you want to see. It's hard to conceptualize what you want and makes it easier to find an answer. – Beartech Nov 07 '16 at 20:44
  • @Beartech See edit again. Im almost there. – Sylar Nov 07 '16 at 21:01
  • You can use the Enumerable `.group_by` on your result but I really can't wrap my head around your data structure as it's all a bit abstract from here. Have you thought about just returning the `f` object to the view and use ERB to loop through the data to break it up by week? – Beartech Nov 07 '16 at 22:04
  • If you choose to use a Database view, check out this answer I gave: http://stackoverflow.com/questions/34205836/rails-group-a-table-by-created-at-returning-a-count-of-the-status-column-and/34245579#34245579 It's a bit more complex than what you are looking for but the gist is that you create an SQL query that returns what you want as a table then build the Rails model to be able to easily access it. – Beartech Nov 07 '16 at 22:06

0 Answers0