Reputation: 42493
HI
whats the best way to implement a simple search in rails to retrieve all models with the same date?
e.g retrieve all customer with birthday date on 23-10-1975, which will be of a date format.
i.e
create_table "customer", :force => true do |t|
t.string "f_name"
t.string "m_name"
t.string "l_name"
t.date "date_of_birth"
end
Upvotes: 0
Views: 1360
Reputation: 7477
Assuming date
is a string of the form 23-10-1975 then something like this;
Customer.all(:conditions => { :date_of_birth => Date.strptime(date, '%d-%m-%Y') })
Upvotes: 1
Reputation: 1216
I presume you want to congratulate all users that have their birthday today? Well, I'm not on a dev-machine now, so I can't test it, but I would go for something like this:
@bday_customers = Customer.find(:all, :conditions => { :birthday => Date.today } )
or even
@bday_customers = Customer.find(:all, :conditions => { :birthday => (Date.today)..(Date.today + 2.weeks) } )
This will cause your database to do the work as it its optimised for such searches.
Upvotes: 3