views:

43

answers:

3

I'm adding data to a db and I only need to check for duplicated data within the last 30 minuets or so.

Data is getting added at about 10 entries per minuet so I only need search about 300 records. How can I get this in conditions with rails Model.find(:all, :conditions => [])?

+3  A: 

Model.find(all, :conditions => ["created_at > ?", 30.minutes.ago])

Jagira
How does the db know if a record was create 30 min ago?
Sam
sql i should say
Sam
While creating a new record, rails by default adds a timestamp to created_at and updated_at columns...
Jagira
internally active record generates SQL query like Select * from TABLE where created_at > (Time.now - 30 minutes)
Jagira
A: 

You should add a condition based on the age of the data. For instance 'created_ad > ?', 30.minutes.ago

Zaki
same thing as with Jagira, does sql have to loop over all records to find the age of the record?
Sam
not if you add an index on that column in the database.
Peter Tillemans
Depends on your table setup. In general yes, but you can add an index (even a clustered one if your db supports it and you don't have other queries/indices) on that column, so the database will not do a full table scan. The usefulness of an index would also depend on the sizes of your rows and the size of your database overall (eg. there is no point in using an index if you don't have enough data in your table)
Zaki
A: 

Your model(sql table) has to have a field called "created_at" that stores the dates:times of the entries being added. Then you can use Jagira's code to check if they are added in the past 30 minutes.

Jagira: Model.find(all, :conditions => ["created_at > ?", 30.minutes.ago])

Gootik