Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between attr_accessor and attr_accessible

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 545
    Comment on it

    The main difference between attr_accessible and attr_accessor is that attr_accessible is a Rails method that basically specifies a white list of model attributes that can be set via mass-assignment and on the other hand attr_accessor is a ruby method that creates getter/setter for the variable

    Example of attr_accessor:

    class Car
      attr_accessor :name
    end
    
    car = Car.new
    car.name = "BMW"
    car.name # => "BMW"
    

    Example of attr_accessible:

    This is mass assignment:

    Message.new(params[:message])
    

    Basically assigning all the attributes to the an instance based on the params[:message] hash.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: