Ruby on Rails - Multiparameter assignment and attr_accessor and 12_hour_time
-
14-11-2019 - |
Question
Ok, I'm slowly getting a grasp on this, but I need some more help.
I'm using a time_select in my view, therefore I'm dealing with a multiparameter assignment. Check.
<%= pt.time_select :time, :twelve_hour => true, :minute_step => 5 %>
BUT I'm doing the naughty thing and using it with an attribute that isn't in the database:
attr_accessor time
Therefore since it can't look at the db, it can't piece together what the multiparameter assignment is supposed to be and therefore I get the following error:
1 error(s) on assignment of multiparameter attributes
Thus I am using information I found here:
composed_of :time,
:class_name => 'DateTime',
:mapping => [%w(DateTime to_s)],
:constructor => Proc.new{ |item| item },
:converter => Proc.new{ |item| item }
Other helpful links: rubyonrails.org | apidock.com
This remove the error, but now the issue is that the plugin I'm using doesn't function properly, I think. I am using http://code.google.com/p/rails-twelve-hour-time-plugin/
. The goal being, I'm trying to get the time select to have 3 drop downs: hh
:mm
am/pm
.
So the question is: how do I need to adjust my composed_of method
in order for it to be properly converted by the plugin? OR is there a better method to this madness?
I'm not sure what mappers / constructors / converters I need. Right now the object keeps the hour and minute, except the hour isn't converted to 24 hour (which the plugin is supposed to take care of, I thought).
Solution
The issue was that I didn't follow the article properly. I should have been using the Time model.
composed_of :time,
:class_name => 'Time',
:mapping => [%w(Time to_s)],
:constructor => Proc.new{ |item| item },
:converter => Proc.new{ |item| item }
Looks like this is a common problem. For more information about it check out this link. The suggestion given there is to do:
class Whatever < ActiveRecord::Base
...
attr_accessor :arrival_time
columns_hash["arrival_time"] = ActiveRecord::ConnectionAdapters::Column.new("arrival_time", nil, "time")
end
I'm still working out bugs, but I'll update this when I get something working.