views:

599

answers:

1

I have a Rails model that should only allow saving/updating of a model once per day per user. I have a callback to do the Find by user and date then add to errors but this is ugly and feels un-rails-like. I have the typical created_at / updated_at columns (and the time portion is significant/I need to keep it).

So I figure I could either:

1) Create another model attribute which is just the date of creation and scope by that (bleh)

2) Use the :scope attribute but somehow get just the date part of created_at, e.g. validates_uniqueness_of :user, :scope => :created_at.to_date (doesn't work, obviously)

3) Validate unless => Proc.new{ |o| Finder that matches my existing callback } (gross)

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086

There will not be an overwhelming number of these, but I'd rather that it is done in SQL instead of Ruby (for obvious scalability reasons).

Any thoughts? Is there a better way?

+1  A: 

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.

Greg Dan