solnic/virtus — Issue #68: Bring back support for mass assignment using accessors?
Author: fgrehm
Created: 2012-02-13 12:36:32+00:00
State: open

Hey folks, we are trying to upgrade virtus from 0.0.9 to 0.2.0 and because of the [changes made to mass assignment](https://github.com/solnic/virtus/compare/v0.0.9...v0.2.0#L29L113) it actually broke some of our specs as we didn't have some of our attributes declared as a virtus attribute, we have our own logic on getters and setters that we think would not fit into an attribute type kinda thing.

To give you some context, we are using virtus for dealing with API resources and we use attributes= to build the object from a JSON response. The reason we believe those accessors wouldn't fit into a attribute type / class is that they are like "virtual properties", used to extract and reconstruct values based on the API data.

The code would be something like this:

class Movie
# ... a bunch of virtus attributes ...
attribute :main_category, String
attribute :subcategory, String

def categories
[self.main_category, self.subcategory]
end

def categories=(all_categories)
self.main_category = all_categories.first
self.subcategory = all_categories.last
end
end

With that we can get extract the "composite" attribute when fetching from the API and simplify our use on the context of our application and rebuild the values when sending data back to the API.

Does it make sense to you guys? If it does, what do you think about it?

Keep up with the good work!
Cheers!