Soh Dubom wrote:
What I'm trying to explain is that through a web interface I can
access web form variables (password, username etc) using the @ sign, eg
: @password, or params[:password], but I'm just using the rails console
trying to interact with a ruby class, can I do that?
@password in a controller/view and params[:password] have nothing to
do with the password attribute of your User model unless you
explicitly assign them to.
So you might do something like this in a controller:
user = User.create(:password => params[:password])
But, all that does 'under the covers' is call
If the user class is `class User << ActiveRecord::Base`, then it will
inherit all sorts of fancy rails behaviour. One such behaviour is that
if there is the users table has a column named 'password', then your
user objects 'automatically' get a method called 'password='. However,
if you overwrite this method (with `def password=`) then you can cause
unexpected problems if you do it wrong.
So the User model inherits from ActiveRecord:Base, and if you have a
'password' column, and if you don't overwrite `def password` or `def
password=`, then you should be able to do this in the rails console:
user = User.new(:password => 'secret')
puts user.password # => secret
user.password = 'hidden'
puts user.password # => hidden
user.save # => true (unless you have validation on this model)
That last line will actually write your values to the database (and
create the new database record).
I should also note, that although most attributes will work just like
I explained above (i.e. all those assumptions I made are true), with
password fields they usually won't work. This is because in order to
be more secure, password fields use all sorts of encryption methods.
Many of these will overwrite `def password=`. A very good example of
this is RESTFul Authentication (look it up on GitHub).
Overall, I would HIGHLY recommend that you read a book like Ruby for
Rails by David Black. This is the best book I can think of that will
help you learn about how Rails uses Ruby to do all this cool stuff.