Reputation: 628
I'm really frustrated with rails because of a ton of little problems like this.
I have a model called timelines
. In it is a datetime column but the javascript that I'm passing the data too is expecting a date only. So I want to add a new string to my model object before passing it to the view. This seems impossible.
First I gather the data from the database and put it into an instance variable:
@timeline = Timeline.where(token: params[:token]).first
Then I try to add on to that variable like this:
@timeline.birthday = "1/1/2000"
I get an error that says:
undefined method birthday= for #<Timeline:0x00000105add0d0>
I'm guessing it's saying this because birthday
is not a column in my database. I know it's not, and I don't care that it's not in my database. I just want to add onto my variable. Is this possible?
Upvotes: 0
Views: 72
Reputation: 367
You could convert the original datetime column to date by using:
your_column_name.strftime("%d/%m/%y")
Upvotes: 0
Reputation: 230346
You need to add accessor methods to be able to get/set birthday. It won't be persisted, though.
class Timeline
attr_accessor :birthday
end
The underlying problem you seem to be having ("javascript expects something else") can be solved in a couple of ways. Presenter pattern, for example. Or use ActiveModel Serializers.
This will allow you to keep the model as is, and, upon rendering, transform data to format expected by client-side.
Upvotes: 2