# Creating multiple objects from form data

**URL:** <https://www.ruby-forum.com/t/creating-multiple-objects-from-form-data/100224>\
**Category:** Rails\
**Created:** [July 27, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/creating-multiple-objects-from-form-data/100224 "2006-07-27T21:28:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 27, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/creating-multiple-objects-from-form-data/100224/1 "2006-07-27T21:28:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 28, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/creating-multiple-objects-from-form-data/100224/2 "2006-07-28T16:15:11Z")

</div>

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:in`create!’  
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:in`with\_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

---

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 28, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/creating-multiple-objects-from-form-data/100224/3 "2006-07-28T16:21:58Z")

</div>

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