Creating multiple objects from form data

I’m in the process of creating a sign up form for an online application.
The form collects account info, company info, and then info for an
administrative user.

The method looks like this:

def create
@account = Account.create!(params[:account])
@company = @account.companies.create!(params[:company])
@user = @company.users.create!(params[:user])
end

However, this inevitably fails on validation and ActionController comes
back
saying that “Validation failed: Username can’t be blank, First name
can’t be
blank, Last name can’t be blank, Email address can’t be blank”

However, values for user are present in the request parameters:

Parameters: {“company”=>{“name”=>“poop”, “account_id”=>29},
“user”=>{“password_confirmation”=>“poop”, “username”=>“poop”,
“company_id”=>29, “time_zone”=>“International Date Line West”,
“first_name”=>“poop”, “password”=>“poop”, “last_name”=>“poop”,
“email_address”=>“poop”}, “account”=>{“name”=>“poop”, “url”=>“poop”}}

I’ve tried using User.new(params[:user]) with the same results. The
only
thing that has given me something different is to loop through the
key/values from params[:user]. I’m a bit stumped here, and any help is
appreciated.

Thanks,

Jeremiah

Curiously enough, when I run this same sequence of code through
script\console, I get the same error.

account = Account.create!(:name => “poop”, :url => “poop”)
company = account.companies.create!(:name => “poop”)
user = company.users.create!(:first_name => “poop”, :last_name =>
“poop”,
:username => “poop”, :email_address => “poop”, :password => “poop”,
:password_confirmation => “poop”)

produces

ActiveRecord::RecordInvalid: Validation failed: Username can’t be blank
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:736:in
save!' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:699:increate!’
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:102:in
method_missing' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:873:inwith_scope’
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:92:in
`method_missing’
from (irb):3

Here’s the code from the user model

require ‘digest/sha2’

class User < ActiveRecord::Base
belongs_to :account
belongs_to :company
belongs_to :department
belongs_to :address
belongs_to :default_project,
:class_name => ‘Project’,
:foreign_key => ‘default_project_id’
has_and_belongs_to_many :roles
has_and_belongs_to_many :projects
has_and_belongs_to_many :meetings

validates_uniqueness_of :username
validates_confirmation_of :password
validates_presence_of :username
#validates_presence_of :password
#validates_presence_of :password_confirmation
#validates_presence_of :first_name
#validates_presence_of :last_name
#validates_presence_of :email_address

cattr_accessor :current_user

attr_accessor :password, :password_confirmation
attr_accessible :password, :password_confirmation

def hashed_password=(pass)
salt = [Array.new(6){rand(256).chr}.join].pack(“m”).chomp
self.password_salt, self.password_hash = salt,
Digest::SHA256.hexdigest(pass

  • salt)
    end

    def self.authenticate(username, password)
    user = User.find(:first, :conditions => [‘username = ?’, username])
    if user.blank? || Digest::SHA256.hexdigest(password +
    user.password_salt)
    != user.password_hash
    raise “Username or password invalid”
    end
    user
    end
    end

Guess I didn’t read my own code close enough. I was blocking myself
with
attr_accessible.