user3765914
user3765914

Reputation: 51

Difference between attribute and attr_accessor in ActiveAttr gem?

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

Answers (1)

user419017
user419017

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

Related Questions