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