Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get records created at the current month?

I have a candidate which has_many votes.

I am trying to get the votes of a candidate that were created in the current month?

@candidate.votes.from_this_month

scope :from_this_month, where("created_at > ? AND created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)

That gives me a PG error: PG::Error: ERROR: column reference \"created_at\" is ambiguous

If I try

scope :from_this_month, where("vote.created_at > ? AND vote.created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)

I get the following error

PG::Error: ERROR:  missing FROM-clause entry for table "vote"
like image 920
grabury Avatar asked Mar 15 '14 11:03

grabury


People also ask

How can I get current month data?

How do I get current month data in SQL? Using MONTH() and GETDATE() function to fetch current month In SQL, we use the GETDATE() method to retrieve the value of the current date which is today's date.

How do you retrieve records based on date?

SQL SELECT DATE is used to retrieve a date from a database. If you want to find a particular date from a database, you can use this statement. For example: let's see the query to get all the records after '2013-12-12'. Let's see the another query to get all the records after '2013-12-12' and before '2013-12-13' date.

How can I get current month data from current date in SQL?

We can retrieve the current month value in SQL using the MONTH() and DATEPART() functions along with the GETDATE() function. To retrieve the name of the month functions in SQL such as DATENAME() and FORMAT() are used.

How do I get previous month data from current month in SQL?

=EOMONTH(date,-2)+1. To get the first day of the previous month for a given date, you can use a simple formula based on the EOMONTH function. The EOMONTH function returns the last day of a month based on a given date. Excel EOMONTH Function.


3 Answers

From Rails 5 you have a much cleaner syntax. On your votes model add a scope this_month

 scope :this_month, -> { where(created_at: Date.today.all_month) }

You can now query @candidates.votes.this_month

like image 128
Joseph N. Avatar answered Oct 25 '22 01:10

Joseph N.


Correct scope

scope :from_this_month, lambda {where("votes.created_at > ? AND votes.created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month)}

This is because in rails the model names are singular(i.e Vote) and tables created are pural (e.g. votes) by convection

EDIT

This can be written simpler with lambda {where(created_at: Time.now.beginning_of_month..(Time.now.end_of_month))} and we need to use lambda due to the reason given in below comments.

Thanx BroiSatse for reminding :D

like image 29
Hardik Avatar answered Oct 24 '22 23:10

Hardik


You need to enclose the where in a lamda as well.

scope :from_this_month, lambda { where("votes.created_at > ? AND votes.created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month) }

Otherwise it may appear to work and your tests will all pass, but if your app runs for more than a month you will start to get incorrect results because Time.now is evaluated when the class loads, not when the method is called.

like image 30
bridiver Avatar answered Oct 25 '22 01:10

bridiver