activerecord - Rails: Getting an array of values from a model for a time period -


Therefore, I have two models, users and things.

  User's_meas: things ended thi is_to: end of user  

talk date associated with it is a user zero one day Things can add, and 4 next, and 7 days after that, then zero for a few days. You get this idea.

Now, what I want to achieve is the array of values ​​for the last 7 days. How many things does the user add? I want to make sure that it contains zero for those days, where nothing was added. For example, this can return [0, 4, 7, 0, 0, 11, 2] .

This is another example which I'm sure it is quite easy to do, but my google-fu is failing me: -)

Thanks!

Rob suggestion will result in 7 questions

How to do this in a query:

  things_by_date = user.things.count (: group = & gt; 'DATE (created_at)' ,: conditions = & gt; ; ['DATE (created_at) & gt ;?', 7.days.ago]) (1..7) .collect {| D | Things_by_date [d.days.ago.to_date.to_s] || 0}  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -