I am happy enough using virtual attributes on a Rails models if they are strings, but I would also like to use other types of attribute, like dates or booleans, so that I can use helpers like date_select to set virtual attributes.
Is there a nice way of doing this?
As it stands, using a date_select helper on a virtual attribute raises:
1 error(s) on assignment of multiparameter attributes
While if I want a boolean attribute I end up with @v_attribute = "true"
, rather than @v_attr = true
.
I found an example somewhere that seemed to work:
class MyModel
#virtual attribute
attr_accessor :v_date_field
attr_accessible :v_date_field
columns_hash["virtual_date_field"] = ActiveRecord::ConnectionAdapters::Column.new("vi_date_field", nil, "date")
end
But I don't really know why it works, whether it has any side effects, and it seems like a hack.
The virtual date / time attribute problem is a known issue in Rails: https://rails.lighthouseapp.com/projects/8994/tickets/2675-support-for-multiparameter-attribute-assignment-on-virtual-attribute-writers
Looks like your solution is the current best option for fixing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With