On 30/11/2010, at 9:56 PM, Rimian Perkins wrote:
> I've created two models, user and socks where a user has many socks. Then, I
> created the migration but when I create a new user and save a new sock, the
> user_id is nil and the validation fails.
Did you save the User first?
The user_id of the user will be nil, until it's saved. So, you need to save it
first, append the sock to the user, then save the sock.
Here's a quick session I did to test it out:
⚡ rails new socks
...
⚡ cd socks
⚡ rails generate model User name:string
invoke active_record
create db/migrate/20101201094919_create_users.rb
create app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
⚡ rails g model Sock color:string user_id:integer
invoke active_record
create db/migrate/20101201094952_create_socks.rb
create app/models/sock.rb
invoke test_unit
create test/unit/sock_test.rb
create test/fixtures/socks.yml
⚡ mate .
here I edited User and Sock to look like this:
class User
has_many :socks
end
class Sock
belongs_to :user
validates_presence_of :user
end
⚡ rake db:migrate
(in /Users/djp/development/socks)
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0013s
== CreateUsers: migrated (0.0016s) ===========================================
== CreateSocks: migrating ====================================================
-- create_table(:socks)
-> 0.0014s
== CreateSocks: migrated (0.0015s) ===========================================
⚡ rails console
Loading development environment (Rails 3.0.0)
ruby-1.9.2-head > u = User.new
=> #<User id: nil, name: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-head > sock = Sock.new
=> #<Sock id: nil, color: nil, user_id: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-head > u.socks << sock
=> [#<Sock id: nil, color: nil, user_id: nil, created_at: nil, updated_at:
nil>]
ruby-1.9.2-head > u.save
=> false
ruby-1.9.2-head > u.save!
ActiveRecord::RecordInvalid: Validation failed: Socks is invalid
ruby-1.9.2-head > u.socks = []
=> []
ruby-1.9.2-head > u.save!
=> true
ruby-1.9.2-head > sock
=> #<Sock id: nil, color: nil, user_id: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-head > u.socks << sock
=> [#<Sock id: 1, color: nil, user_id: 1, created_at: "2010-12-01 09:52:05",
updated_at: "2010-12-01 09:52:05">]
ruby-1.9.2-head > sock.save
=> true
ruby-1.9.2-head > Sock.first
=> #<Sock id: 1, color: nil, user_id: 1, created_at: "2010-12-01 09:52:05",
updated_at: "2010-12-01 09:52:05">
ruby-1.9.2-head >
'rails console' is a very handy tool for exploring these sorts of things.
In summary, the order of things is:
u = User.new
u.socks = []
u.save!
sock = Sock.new
u.socks << sock
sock.save
--
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rails-oceania?hl=en.