Scoping Date Attribute for This Week

Scoping date attribute for this week?

at_beginning_of_week has been removed in Rails 3. You should use beginning_of_week but, be careful, it's an instance method. So you have to do something like:

Date.today.beginning_of_week

Furthermore, you can use a range and make your query very nice to read:

where(:purchase_date => Date.today.beginning_of_week..Date.today.end_of_week)

Ruby on rails. Update attribute when date passes

It's relevant to know the purpose of the boolean. If your goal is to be able to create a property to use in where queries throughout your code (e.g., getting all expired gigs), then a scope might be a good way to go. Just add this code to your Gig model:

scope :expired, -> { where('date < ?', Time.current.beginning_of_day) }

You could then get all the expired gigs by writing:

Gig.expired

Then you don't need the expired boolean at all. If you use your current method approach, you'd have to use a background process to set expired booleans everyday. See http://guides.rubyonrails.org/active_record_querying.html#scopes for more information on using scopes.

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

How can I compare an attribute of a instance, which is a date, to the current date?

scope :last_week, lambda {where("line_item.last_eaten >= ?", 7.days.ago)}

should work...

But further reading made me realise that your table is called line_items, not line_item

When you're doing sql-snippets, you need to refer to the name of the table in SQL, rather than treating it like an individual rails object's name. This means always use the pluralised version :)

Rails: Validates uniqueness of scoped by date

You can also write your own validates method. This is quite easy. And in the custom validate method, date_trunc (Postgresql) can be used to find existing records in required time span. date_trunc can be also parametrized (e.g.hour, day, week, month).

Instead of date_trunc a simple condition can be used to find conflicting record. e.g.
["user_id = ? AND updated_at >= ?", self.user_id, Time.now.beginning_of_day] And I guess this record look up should be faster because it can use an index.

ActiveRecord Find By Year, Day or Month on a Date field

Assuming that your "date attribute" is a date (rather than a full timestamp) then a simple where will give you your "find by date":

Model.where(:date_column => date)

You don't want find_by_date_column as that will give at most one result.

For the year, month, and day queries you'd want to use the extract SQL function:

Model.where('extract(year  from date_column) = ?', desired_year)
Model.where('extract(month from date_column) = ?', desired_month)
Model.where('extract(day from date_column) = ?', desired_day_of_month)

However, if you're using SQLite, you'd have to mess around with strftime since it doesn't know what extract is:

Model.where("cast(strftime('%Y', date_column) as int) = ?", desired_year)
Model.where("cast(strftime('%m', date_column) as int) = ?", desired_month)
Model.where("cast(strftime('%d', date_column) as int) = ?", desired_day_of_month)

The %m and %d format specifiers will add leading zeroes in some case and that can confuse the equality tests, hence the cast(... as int) to force the formatted strings to numbers.

ActiveRecord won't protect you from all the differences between databases so as soon as you do anything non-trivial, you either have to build your own portability layer (ugly but sometimes necessary), tie yourself to a limited set of databases (realistic unless you're releasing something that has to run on any database), or do all your logic in Ruby (insane for any non-trivial amount of data).

The year, month, and day-of-month queries will be pretty slow on large tables. Some databases let you add indexes on function results but ActiveRecord is too stupid to understand them so it will make a big mess if you try to use them; so, if you find that these queries are too slow then you'll have to add the three extra columns that you're trying to avoid.

If you're going to be using these queries a lot then you could add scopes for them but the recommended way to add a scope with an argument is just to add a class method:

Using a class method is the preferred way to accept arguments for scopes. These methods will still be accessible on the association objects...

So you'd have class methods that look like this:

def self.by_year(year)
where('extract(year from date_column) = ?', year)
end
# etc.

How to change the scope of the function passed in to the eventClick property on a FullCalendar object?

The solution was just using bind when passing the function to the object property.
So in code it would look like this:

clickEvent: me._onEventClick.bind(me)

Here "me" gets set to "this" in _initializeCalendar, so "this" has the global scope.



Related Topics



Leave a reply



Submit