I am sure I am just doing this wrong, can someone point me in the right direction?
I need to set the value of effective_date to a modified format date
effective_date is an attribute of Request
class Request < ActiveRecord::Base
def format_date=(format_date)
date_a = format_date.split("/")
month, day, year = date_a
effective_date = Date.parse("#{year}-#{month}-#{day}")
end
def format_date
effective_date
end
end
Request.create(format_date: "04/21/2012") is not setting the value to effect_date
Edit: I guess this doesn’t make sense, so I will try and explain it better:
I need to set the value of effective_date (a column in the database) from format_date (not a column in the database). I am using format_date method to convert the date from format_date and store it into effective_date. If this isn’t enough info, let me know. I am not sure what else to add.
If
effective_dateis the column name in your database, the change is as simple asThe only change was
effective_date =toself.effective_date =. Without addingselfthere, there’s no way for the interpreter to know whether you want to set a local variable calledeffective_dateor call theeffective_date=method. It assumes the local variable.