Rails composed_of
I learned that Rails ActiveRecord has composed_of
method which allow us to group model attributes under a common PORO like object. Let's say we have an User
with address_street
and address_city
fields, stored in the same table, so we can still group the address attributes:
class User < ActiveRecord::Base
composed_of :address, mapping: [%w(address_street street), %w(address_city city)]
end
This way, besides the user.address_street
and user.address_city
, we can also access the same values as:
address = user.address
puts address.street
puts address.city
Tweet