Forum: Ruby on Rails When adding a record in console, a parameter comes in as nul

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-06 01:34
(Received via mailing list)
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 => "support@mydomain.com", :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"=>"support@mydomain.com"},
@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]*[a-zA-Z0-9])*
    (\.[a-zA-Z0-9]([-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
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-06-09 03:22
(Received via mailing list)
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 => "ddd@ddd.com"
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]*[a-zA-Z0-9])*
    (\.[a-zA-Z0-9]([-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


==============
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-06-09 03:31
(Received via mailing list)
Hello Jeff !

2006/6/5, Jeff Ward <animikii@gmail.com>:
> User.create :name => "Administrator", :username => "admin", :password =>
> "1234", :email => "support@mydomain.com", :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 => "support@mydomain.com"
u.account_id = 1
u.save!

you'll be fine.

Some related info:
#attr_accessible:
http://api.rubyonrails.com/classes/ActiveRecord/Ba...

#attr_protected (the reverse of attr_accessible):
http://api.rubyonrails.com/classes/ActiveRecord/Ba...

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

Hope that helps !
This topic is locked and can not be replied to.