1

I am planning to use conditional validations along the lines of what is described in this railscast In the railscast, which is rather old, attr_accessor is used, (skip to the later portion of the video to see the code). I am relatively new to rails programming and wanted to read up on what attr_accessor does. This post asks about using it and the most upvoted answer says that they should hardly ever be used in rails.

Is it necessary/should I be using attr_accessor like is done in the railscast? Or are these methods automatically created? Is there any danger to using attr_accessor in this case?

Community
  • 1
  • 1
John
  • 13,125
  • 14
  • 52
  • 73
  • 1
    The `attr_accessor` fields shown *are not directly related to the database* as pointed out in the linked answer. It is just a shortcut for creating getters/setters to instance variables: that is, other *transient* bits (not backed by the DB) of the model. (Instance variables cannot be [easily] set from outside an instance in Ruby.) –  Jan 15 '12 at 22:01
  • The linked posts most up-voted argues that this transient nature is normally not "correct" as it is not persisted and therefore does not belong in the model objects. However, this does not answer the questions you ask, which seem confused about what `attr_accessor` does :) –  Jan 15 '12 at 22:06
  • In the linked post, @Peter says "Automatic database-backed accessors are created for you. . ." Does he mean that when you create attributes that are stored in the database; an accessor is automatically created for you? I was a little confused. – John Jan 15 '12 at 22:12
  • 1
    @John Yes. `attr_accessor` is used for non-persistent attributes. – Dave Newton Jan 15 '12 at 22:13
  • @pst, Yes, I am curious about what it does (though I think I understand it makes an accessor method), and more so, when it's automatically done. – John Jan 15 '12 at 22:14
  • @John Yes, AR will create the database-backed accessors automatically. –  Jan 15 '12 at 22:38

1 Answers1

4

Using attr_accessor has nothing to do with Active Record. I discuss how it works in this post, that is also related to AR.

Now, what AR does do, is it automatically creates is own "accessor" methods (e.g. x/x=) based on the database model. These automatically created methods are really just stubs that it uses to proxy into the internal AR workings.

The point is, attr_accessor (automatically) wraps simple instance variable access, while AR (automatically) created methods wrap AR magic. The two operations are mutually exclusive. Because attr_accessor does not "link to" the AR magic, all it can be used for creating transient fields which are not persisted: AR does not know of or care about instance variables.

The "danger" comes from perhaps complicating the model objects with transient information -- if it is transient, why should it be part of a model object? This is the argument the most up-voted answer in the linked question makes.

Happy coding.


However, I do no know what would happen if using attr_accessor for the same field as that which happens to be in the AR model... confusion at the least.

Community
  • 1
  • 1