# When adding a record in console, a parameter comes in as nul

**URL:** <https://www.ruby-forum.com/t/when-adding-a-record-in-console-a-parameter-comes-in-as-nul/62546>\
**Category:** Rails\
**Created:** [June 6, 2006, 1:34am UTC](https://www.ruby-forum.com/t/when-adding-a-record-in-console-a-parameter-comes-in-as-nul/62546 "2006-06-06T01:34:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 6, 2006, 1:34am UTC](https://www.ruby-forum.com/t/when-adding-a-record-in-console-a-parameter-comes-in-as-nul/62546/1 "2006-06-06T01:34:00Z")

</div>

In console, I am trying to create a User but the :account\_id does not  
come  
in. Console just gives me back :account\_id =\> nil. Obviously I’m trying  
to  
set it though. Silly console…

But, I can set the account\_id column in my controller like so:

@user = User.new(params[:user])  
@user.account\_id = account.id  
@user.save

Here’s what I give to the controller:

User.create :name =\> “Administrator”, :username =\> “admin”, :password =\>  
“1234”, :email =\> “removed\_email\_address@domain.invalid”, :account\_id =\> 1

Console gives me:

=\> #\<User:0x3b55158 @attributes={“name”=\>“Administrator”,  
“account\_id”=\>nil,  
“hashed\_password”=\>“7110eda4d09e062aa5e4a390b0a572ac0d2c0220”,  
“username”=\>“admin”, “id”=\>8, “created\_at”=\>Mon Jun 05 16:13:18 Pacific  
Standard Time 2006, “email”=\>“removed\_email\_address@domain.invalid”},  
@errors=#\<ActiveRecord::Errors:0x3b51e88 @base=#\<User:0x3b55158 …\>,  
@errors={}\>, @new\_record=false, @password=nil\>

I am doing some things in the user.rb Model file and I think it has  
something to do with the attr\_ bits which I don’t fully understand.

my user.rb file… any ideas?

require “digest/sha1”  
class User \< ActiveRecord::Base  
belongs\_to :account  
attr\_accessor :password  
attr\_accessible :username, :password, :email, :name

validates\_uniqueness\_of :username, :scope =\> :account\_id  
validates\_presence\_of :username, :password, :name, :email, :account\_id  
validates\_format\_of :email, :with =\> /  
^[-^!$#%&’_+/=?`{|}~.\w]+  
@a-zA-Z0-9_  
(.a-zA-Z0-9\*)+$/x,  
:message =\> “must be a valid email address”,  
:on =\> :create

def before\_create  
self.hashed\_password = User.hash\_password(self.password)  
end  
def after\_create  
@password = nil  
end  
def self.hash\_password(password)  
Digest::SHA1.hexdigest(password)  
end  
def self.login(username, password, account\_id)  
hashed\_password = hash\_password(password || “”)

find\_by\_username\_and\_hashed\_password\_and\_account\_id(username,hashed\_password,account\_id)  
end  
def try\_to\_login(account\_id)  
User.login(self.username, self.password, account\_id)  
end  
end

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 3:22am UTC](https://www.ruby-forum.com/t/when-adding-a-record-in-console-a-parameter-comes-in-as-nul/62546/2 "2006-06-09T03:22:35Z")

</div>

Okay, so I found what seems to be a hack to get this to work…

user = User.create :name =\> “aaa”, :username =\> “bbb”, :password =\>  
“1234”, :email =\> “removed\_email\_address@domain.invalid”  
user.account = Account.find(1)  
user.password = “1234”  
user.save

It needed the password before it would let me save the user.account.  
Probably because of the hashed password stuff I did. I don’t fully  
understand the attr\_accessor stuff. But this is my model file…

===============

require “digest/sha1”  
class User \< ActiveRecord::Base  
belongs\_to :account  
attr\_accessor :password  
attr\_accessible :username, :password, :email, :name

validates\_uniqueness\_of :username, :scope =\> :account\_id  
validates\_presence\_of :username, :password, :name, :email  
validates\_format\_of :email, :with =\> /  
^[-^!$#%&’_+/=?`{|}~.\w]+  
@a-zA-Z0-9_  
(.a-zA-Z0-9\*)+$/x,  
:message =\> “must be a valid email address”,  
:on =\> :create

def before\_create  
self.hashed\_password = User.hash\_password(self.password)  
end  
def after\_create  
@password = nil  
end  
def self.hash\_password(password)  
Digest::SHA1.hexdigest(password)  
end  
def self.login(username, password, account\_id)  
hashed\_password = hash\_password(password || “”)  
find\_by\_username\_and\_hashed\_password\_and\_account\_id(username,hashed\_password,account\_id)  
end  
def try\_to\_login(account\_id)  
User.login(self.username, self.password, account\_id)  
end  
end

==============

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 3:31am UTC](https://www.ruby-forum.com/t/when-adding-a-record-in-console-a-parameter-comes-in-as-nul/62546/3 "2006-06-09T03:31:43Z")

</div>

Hello Jeff !

2006/6/5, Jeff W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> User.create :name =\> “Administrator”, :username =\> “admin”, :password =\>  
> “1234”, :email =\> “removed\_email\_address@domain.invalid”, :account\_id =\> 1

> class User \< ActiveRecord::Base  
> attr\_accessible :username, :password, :email, :name  
> end

#attr\_accessible tells AR which attributes are available through  
mass-assignment. Since account\_id is NOT in the attr\_accessible list,  
AR prevents it’s value from being set.

If instead you do  
u = User.new :name =\> “Administrator”, :username =\> “admin”,  
:password =\> “1234”, :email =\> “removed\_email\_address@domain.invalid”  
u.account\_id = 1  
u.save!

you’ll be fine.

Some related info:  
#attr\_accessible:  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000874](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000874)

#attr\_protected (the reverse of attr\_accessible):  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000873](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000873)

Trevor S. quiz:  
[http://tinyurl.com/prxdl](http://tinyurl.com/prxdl)

Hope that helps !
