Howto: Model Scope for Todays Records

Howto: Model scope for todays records

Since "created_at" column contains date and time, but you need compare only date, you have two ways (I assume you use MySQL) :

  1. use BETWEEN:

    scope :today, lambda { WHERE("created_at BETWEEN '#{DateTime.now.beginning_of_day}' AND '#{DateTime.now.end_of_day}'") }
  2. use DATE() function:

    scope :today, lambda { WHERE('DATE(created_at) = ?', Date.today)}

also, you can add "created_on" column to the table with date only.

Updated:

def self.up
add_column table_name, :created_on, :date
add_column table_name, :updated_on, :date
end

scope :today, lambda { where(created_on: Date.today) }

ActiveRecord scope to find records created today?

You will need to consider user's timezone. That is, beginning of day and end of day in the user's timezone. For that, first parse the current time in user's timezone. You can set the zone to user's timezone and do the calculation or you can parse the time directly using a variation of the following code.

timezone = current_user.timezone # Mountain Time (US & Canada)
users_current_time = ActiveSupport::TimeZone[timezone].parse(Time.now.to_s)

users_current_time.beginning_of_day and users_current_time.end_of_day should generate the right time range (which you app would convert to UTC and fire the query, if UTC is your applications timezone)

If you want to do that with scopes only, then I suggest you set the Time.zone = current_user.timezone before_filter of all actions and use it in the scope. This
railscast gives fair idea about how to go about doing that.

Rails - What's the proper way to use scopes to check if a record exists for the current day only

The whole Time.now.beginning_of_day..Time.now.end_of_day sounds overcomplicated to me. How about you store created_on just like created_at, but a Date, not a DateTime. Your uniqueness scope becomes much easier, similarly creation could be:

current_user.logs.where(keyword: query_value, created_on: Date.today).first_or_create(other_fields)

I'm assuming user has_many :logs, for readability. Instead of UserLog.create(user_id: current_user.id, ...

best way to get records created today with Ecto Phoenix?

Your best bet would be to use the Timex Library.

Using this library you could do something like this:

date = DateTime.today |> Timex.datetime

query = from m in Model, where: m.inserted_at >= ^Timex.beginning_of_day(date), where: m.inserted_at <= ^Timex.end_of_day(date)

Repo.all(query)

Rails scope for values only between two dates

You can do this:

scope :activeDate, -> { where("? BETWEEN startDate AND endDate", Time.now.to_date)}

Since you are using greater or equal AND lesser or equal, you can use the equivalent SQL function "BETWEEN".

You don't need to convert the Time to a date,you can directly call the Date.today:

scope :activeDate, -> { where("? BETWEEN startDate AND endDate", Date.today)}

You could improve your scope by doing this:

scope :activeAtDate, lambda{ |date = Date.today| where("? BETWEEN startDate AND endDate", date) }

And use it like this:

Product.activeAtDate() #=> use the Date.today
Product.activeAtDate(1.week.ago.to_date) #=> use Today - minus 7 days

Count records created within the last 7 days

You can add a where-condition like this:

self.favorites.where('created_at >= ?', 1.week.ago).count

And for your calculate_user_score method, you probably want to do that for links as well:

def calculate_user_score
unless new_record?
self.score = (links.where('created_at >= ?', 1.week.ago).count * 5) +
(favorites.where('created_at >= ?', 1.week.ago).count * 0.5)
end
end

How to get records created at the current month?

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.



Related Topics



Leave a reply



Submit