Difference between attr_accessor and attr_accessible

attr_accessor is a Ruby method that is used to create accessors (getters and setters).

It’s the equivalent to doing this:

class Person

    def name

        @name

    end

    def name=(name)

        @name = name

    end

end

attr_accessible is a Rails method that allows you to pass in values to a mass assignment – e.g. new(attrs) or update_attributes(attrs)

Here’s an example of a mass assignment:

Person.new ({ :name => “Adam”, id => 4 })

By using 

attr_accessible :name

you could limit what is updated to just the name field rather than the id as well.

So, in a nutshell, attr_accessor is about creating methods to store variables and attr_accessible is about the security of mass assignments.

http://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible

Leave a Reply

Your email address will not be published. Required fields are marked *