Hey all,

Let's say you have this method:

def signup
  @user = User.new(params[:user])
  if request.post?
    if @user.save
      session[:user] = User.authenticate(@user.login, @user.password)
      flash[:message] = "Signup successful"
      redirect_to :action => "welcome"
    else
      flash[:warning] = "Signup unsuccessful"
    end
  end
end

Since User class inherits from ActiveRecord class, I presume
ActiveRecord contains a constructor that takes the key/value pairs of a
hash from the parameters of web form and checks if the keys of the hash
match the instance methods available in current class instance which
were generated from field names from a table of the same name (e.g.
Users).

Basically all that happening with this line:
  @user = User.new(params[:user])

Hence, you can now do @user.login - should login be a field in the
database. If a value for login was captured in the param hash, then
@user.login will return value passed from params hash.

Does anyone have a general description of what ActiveRecord does behind
the scenes to achieve this?


Now the second question. What about parameter's that are not part of the
table field names? ActiveRecord doesn't create getter and setter methods
for these parameters by default.

You have to manually do it:

attr_accessor :password #should a password field not exist in users
table

So basically when a new instance is created, and we accept a hash
parameter, which contains a password key that doesn't translate to field
in database, ruby immediately checks if such a value has a setter and
getter (attr_accessor :password) and if it does, then it calls the below
method since it needs to resolve the password object:

def password=(pass)
       @password=pass
       self.salt = User.random_string(10) if !self.salt?
       self.hashed_password = User.encrypt(@password, self.salt)
     end

So is that all that happens here or does ActiveRecord do something else
behind the scenes? This is a different question from my first question.

ALso, why do you think this method is authenticating the user as soon as
they are created? The fact that they just been created suggests that
they are real.

Thanks for response.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to