I have records I need to order with a date attribute. In my code I call:
records.order("`records`.date esc")
however this doesn't seem to work.
If I run:
records.order("`financial_transactions`.date asc").pluck(:date)
I get:
[Fri, 09 Dec 2016, Wed, 07 Dec 2016, Sun, 25 Jun 2017, Sat, 25 Mar 2017]
which is obviously incorrect. I also get the same result if I run:
records.order("`records`.date desc").pluck(:date)
How can I fix this?
If you are on Rails 4+, this should work for descending order:
records.order(date: :desc).pluck(:date)
And if you want them in ascending order, you can just do:
records.order(:date).pluck(:date)
For Rails 3.2:
records.order('records.date desc').pluck(:date)
records.order('records.date asc').pluck(:date)
If you are on Rails 5, try
records.order(date: :desc).pluck(:date)
If you want them ascending, do this:
records.order(date: :asc).pluck(:date)
Related
When I show the total expend of PUQ theres no problem
Expend.where(cc: "PUQ").sum(:total)
But when I want to show by month individually with this code, I Don't get the expenses of the 31, only show 1 to 30 of the month. Right now I dont get the expenses of 31-08-2020
Expend.where(cc: "PUQ").where(:created_at => ((Date.today.beginning_of_month)- 1.month)..((Date.today.end_of_month) - 1.month)).sum(:total)
Thanks in advance
You can use these handy Rails methods
range = Time.current.advance(months: -1).all_month
# => Sat, 01 Aug 2020 00:00:00 UTC +00:00..Mon, 31 Aug 2020 23:59:59 UTC +00:00
Expend.where(created_at: range)
More about all_month
and about advance
I've getting this problem just once after updating the record, after updated record if i try same things over again there is no problem
For example i have a prices table for my products, and it is nested in products
when i make changes for products table itself, my price nested field triggers changes also example below when i debug with binding.pry;
[1] pry(#<Price>)> changes
=> {"prices"=>[[#<BigDecimal:7fea13b5ddd0,'0.16E1',18(18)>], [#<BigDecimal:7fea13afc698,'0.16E1',18(18)>]],
"updated_at"=>[Wed, 05 Sep 2018 16:14:07 BST +01:00, Thu, 27 Sep 2018 09:57:49 BST +01:00]}
[2] pry(#<Price>)> dd
NameError: undefined local variable or method `dd' for #<Price:0x00007fea13afe650>
from /Users/user/.rvm/gems/ruby-2.3.7/gems/activemodel-4.2.10/lib/active_model/attribute_methods.rb:433:in `method_missing'
[3] pry(#<Price>)> changes
=> {"prices"=>[[#<BigDecimal:7fe9f5623748,'0.16E1',18(18)>], [#<BigDecimal:7fea13afc698,'0.16E1',18(18)>]],
"occured_dates"=>[[Wed, 05 Sep 2018], [Wed, 05 Sep 2018]],
"updated_at"=>[Wed, 05 Sep 2018 16:14:07 BST +01:00, Thu, 27 Sep 2018 09:57:49 BST +01:00]}
change triggers might be my codes but wierd thing when i send wrong method to active_model in binding.pry console it gives me more changed attributes after that even there is no action happend between.
Edit:
problem was cause by BigDecimal keep creating new BigDecimal on the progress
I'm trying to get the last day of last week.
Today is:
d = Date.today
=> Sun, 22 Nov 2015
d.at_end_of_week
=> Sun, 22 Nov 2015
d.at_end_of_week.last_week
=> Mon, 09 Nov 2015
The last command should return Mon, 15 Nov 2015, no?
This also returns the same FYI:
Date.today.at_end_of_week.last_week
=> Mon, 09 Nov 2015
What am I missing here?
What you are seeing is correct. Note that last_week is an alias of prev_week which takes an optional parameter that defaults to Date.begining_of_week (:monday)
Resulting in trying to find the date in the previous week but from the past monday.
If instead you call Date.today.at_end_of_week.last_week :sunday. For what you try to accomplish you need a way to replace :sunday with something based on today's date. Maybe this but I haven't tried it.
Try:
Date.today.last_week.at_end_of_week
I am working with rails and postgresql. I have a created_at timestamp on a model. I am in a situation where I need to order the results by second. I can't seem to get rails todo this.
e.g. If I insert 5 records in 1 minute, and then want to sort by created_at time, since they all have the same minute, it starts to order them by alpha because they all have the same insert minute.
How can I make it so that it orders by the insert in seconds. Postgres is storing it. But DateTime doesn't seem to take seconds into account in this way...
I hope this makes sense
Kirk
ActiveRecord order method should sort including the second
Eg:
Note the last two datetimes are in the same minute
User.pluck(:created_at)
=> [Fri, 14 Mar 2014 16:26:02 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 18:33:51 UTC +00:00]
User.order("created_at desc").collect(&:created_at)
=> [Fri, 14 Mar 2014 18:33:51 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 16:26:02 UTC +00:00]
Adding:
default_scope -> { order('created_at DESC') }
to your ActiveRecord should do it because created_at should look like this:"2014-03-12 15:29:26" which specifies the seconds.
I have a very weird requirement where I need to deal with months. Here is what I am try to do actually. I have two object say
jan_start_date=Time.parse("2012-01-01 00:00:00")
jan_end_date=Time.parse("2012-01-31 23:59:59")
I take this two datetime objects and iterate over feb, mar, april and soon to get some data.
Then I will take feb start and end dates and iterate over march, april and soon.
Its like month on month data collection. Once I am done with say Jan data, I need to take Feb's data and so on and so forth. How do I achieve this. Since I need to iterate over months.
Kindly help me out
Thanks
ActiveSupport has beginning_of_month and end_of_month:
d = Date.today
#=> Fri, 13 Jul 2012
d.beginning_of_month
#=> Sun, 01 Jul 2012
d.end_of_month
#=> Tue, 31 Jul 2012
You can use Date#>> to shift dates forward monthwise:
(d>>2).end_of_month
#=> Sun, 30 Sep 2012
(d>>4).beginning_of_month
#=> Thu, 01 Nov 2012