views:

97

answers:

1

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.

+1  A: 

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.

malclocke