How to retrieve console input in a ruby class

How to retrieve console input in a ruby class

Hi. Suppose I have a db table 'users': username, password, email etc and a ruby class User. In the rails console I type:

u = User.new u.password = '123' ... u.save

From inside the user.rb class, how can I request the user attributes like password?

At this point I don't have the the view done so I not considering I'm getting user input through a web form. In this case I can get user input through the @password keyword, but what about the other case?

In the before_create I could do that

  def before_create     self.password = User.hash_password(@password)   end

but that will work only for web form submit!

Soh Dubom wrote: > In the before_create I could do that

  def before_create     self.password = User.hash_password(@password)   end

but that will work only for web form submit!

I think you're wrong. password= is probably just an accessor, setting @password. So, u.password = '123' sets @password to '123. u.password returns the value of @password. password= _could_ be anything, though. It depends on the implementation.

August Lilleaas wrote:

Soh Dubom wrote: > In the before_create I could do that

  def before_create     self.password = User.hash_password(@password)   end

but that will work only for web form submit!

I think you're wrong. password= is probably just an accessor, setting @password. So, u.password = '123' sets @password to '123. u.password returns the value of @password. password= _could_ be anything, though. It depends on the implementation.

I understand password is a property of User class (as username, email etc). 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?

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 user.password=(params[:password])

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.

HTH

/journeys 539 > script/console Loading development environment (Rails 2.1.1)

user = User.find(:first)

=> #<User id: 1, username: "Admin", email: "admin@railscoders.net", hashed_password: "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81...", enabled: true, profile: "Site Administrator", created_at: "2008-09-21 07:06:44", updated_at: "2008-09-23 10:31:52", last_login_at: nil, posts_count: 0, entries_count: 0, blog_title: "Big Kahuna", enable_comments: true, photos_count: nil>

exit

/journeys 540 >