Rails ActiveRecord search records between two dates

Yeah, this is simple in Rails. date = Date.today start_date = date.at_beginning_of_month end_date = date.at_end_of_month Model.where(:created_at => start_date..end_date) This gives the records created in current month. This is simple because rails save created_at and updated_at in following format: created_at: “2018-09-03 09:49:41” But here in my case,  I had to find the records that are valid…

Read more...

Rails Tips: ActiveRecord Conditional Validation

In my User model, I have to check validation presence of state only for users from Nepal. No need to check validation for the users from other country. For this I have implemented as follows: class User < ActiveRecord::Base validates_presence_of :state, :if => :in_nepal? private def in_nepal? country == ‘Nepal’ end end Here country is…

Read more...