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

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 => “[email protected]”, :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”=>“[email protected]”},
@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

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 => “[email protected]
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

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

Hello Jeff !

2006/6/5, Jeff W. [email protected]:

User.create :name => “Administrator”, :username => “admin”, :password =>
“1234”, :email => “[email protected]”, :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 => “[email protected]
u.account_id = 1
u.save!

you’ll be fine.

Some related info:
#attr_accessible:
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

Trevor S. quiz:
http://tinyurl.com/prxdl

Hope that helps !