# How to retrieve console input in a ruby class

**URL:** <https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878>\
**Category:** Rails\
**Created:** [October 27, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878 "2008-10-27T14:05:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 27, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878/1 "2008-10-27T14:05:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 27, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878/2 "2008-10-27T15:00:53Z")

</div>

Soh D. 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.

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 27, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878/3 "2008-10-27T15:11:12Z")

</div>

August L. wrote:

> Soh D. 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?

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 27, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878/4 "2008-10-27T15:59:37Z")

</div>

Soh D. 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

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [October 27, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-console-input-in-a-ruby-class/151878/5 "2008-10-27T19:35:15Z")

</div>

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

> > user = User.find(:first)  
> > =\> #\<User id: 1, username: “Admin”, email: “removed\_email\_address@domain.invalid”,  
> > 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 \>

On Oct 27, 3:05 am, Soh D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
