Reputation: 51
I'm trying to use the active_attr gem to create models backed by a NoSQL database that doesn't seem to have any other ORMs or mappers that fit our needs.
In the documentation (https://github.com/cgriego/active_attr), it shows examples defining attribtues with just attribute
and sometimes using attr_accessor
. I'm not quite sure I see the difference. Can someone explain when I should use one over the other?
Upvotes: 2
Views: 2437
Reputation:
attr_accessor
is a Ruby method, attribute
is a custom method for active_attr
.
For example:
class User
include ActiveAttr::QueryAttributes
attribute :first_name
end
User.new.first_name?
In the above, attribute :first_name
will use attr_accessor
to create basic getters/setters (first_name
and first_name=
), and then additionally add at least another method first_name?
.
It appears attr_accessor
is used alongside modules that augment the Class (MassAssignment
, BlockInitialization
), whereas attribute
is used for modules that directly augment attributes of the Class instances.
Upvotes: 4