Reputation: 351
I have a problem, I want to update my database's "active" column. my schema =
tablename(name:string , id:int , active:0 ,created_at)
I want to update active attribute in my rails app after a certain period of time let say 2 days from the created_at time
I have tried few stuff like this from controller but none of this worked for me.. can anyone help ?
def check_aim
@aim = Aim.find(1)
aimdays = @aim.day
futuredate = @aim.created_at+aimdays.days
if(Time.now == futuredate)
@aim.active = false # i have tried @aim.update_attribute(:active,1) also didn't word either
@aim.save
end
if @aim.save
saved
else
not saved
end
end
helper_method :check_aim
view class
=debug check_aim
this returns
saved
but when i see my database nothing has changed ... plz help
Upvotes: 0
Views: 173
Reputation: 351
I figured it out.. stupid rails
Guys this is the most stupid thing by rails... it was nothing but the problem of keyword 'active' i changed column name from active to status and now it works fyn.. damm it !!! I wasted hours on this :/
Upvotes: 0
Reputation: 23711
There is possibility that the condition Time.now == futuredate
is failing but still @aim.save
will return you true
as it can be saved with no change.
You need to change it to Time.now >= futuredate
so if the futuredate
is passed the active
will be set to 0
You may move the puts
statements inside the block and check whether it's printing something
def check_aim
@aim = Aim.find(1)
aimdays = @aim.day
futuredate = @aim.created_at + aimdays.days
if(Time.now >= futuredate)
@aim.active = 0 # i have tried @aim.update_attribute(:active,1) also didn't word either
if @aim.save
"saved"
else
"not saved"
end
end
end
Upvotes: 1
Reputation: 3018
To genuinely update a column after a certain amount of time, one way would be to run a cron script. The script could run every day to check the table and update the active
fields that are not set to active 2 days after the created_at
date.
Upvotes: 2