Ruby on rails 3 使用所属对象创建具有多个关联

Ruby on rails 3 使用所属对象创建具有多个关联,ruby-on-rails-3,Ruby On Rails 3,在我的规范中执行@user.posts.create时,我得到了错误未知属性:user\u id 用户类 class User < ActiveRecord::Base # new columns need to be added here to be writable through mass assignment attr_accessible :username, :email, :password, :password_confirmation has_many :p

在我的规范中执行@user.posts.create时,我得到了错误未知属性:user\u id

用户类

class User < ActiveRecord::Base
  # new columns need to be added here to be writable through mass assignment
  attr_accessible :username, :email, :password, :password_confirmation

  has_many :posts, :dependent => :destroy
end
有什么帮助吗?我遵循了我能找到的关于使用ActiveRecord的所有指南。我所要做的就是创建一个有关联用户的帖子。

您可以使用

@user = User.find(1)
@post = @user.posts.build(posts_attributes_as_hash)
@post.save 
甚至

post = Post.new(posts_attributes)
@user = User.find(1)
@user.posts << post
有关更多信息,请查看has_many添加的名为4.3.1方法的章节

新编辑:

我用您的代码创建了一个新项目,并在rails控制台中尝试了以下命令

User.create(:username => "UserNamedTest", :email => "usernamedtest@somewhere.com")

SQL (13.6ms)  INSERT INTO "users" ("created_at", "email", "password_hash", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Wed, 14 Dec 2011 09:02:46 UTC +00:00], ["email", "usernamedtest@somewhere.com"], ["password_hash", nil], ["password_salt", nil], ["updated_at", Wed, 14 Dec 2011 01:03:26 UTC +00:00], ["username", "UserNamedTest"]]

=> #<User id: 2, username: "UserNamedTest", email: "usernamedtest@somewhere.com", password_hash: nil, password_salt: nil, created_at: "2011-12-14 09:02:46", updated_at: "2011-12-14 09:02:46">

user =  User.find_by_username("UserNamedTest")

User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'UserNamedTest' LIMIT 1

=> #<User id: 2, username: "UserNamedTest", email: "usernamedtest@somewhere.com", password_hash: nil, password_salt: nil, created_at: "2011-12-14 09:02:46", updated_at: "2011-12-14 09:02:46">

new_post = user.posts.create(:title => "just a test", :body =>"body of article test")

SQL (0.5ms)  INSERT INTO "posts" ("body", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["body", "body of article test"], ["created_at", Wed, 14 Dec 2011 09:03:59 UTC +00:00], ["title", "just a test"], ["updated_at", Wed, 14 Dec 2011 09:03:59 UTC +00:00], ["user_id", 2]]

=> #<Post id: 2, title: "just a test", body: "body of article test", user_id: 2, created_at: "2011-12-14 09:03:59", updated_at: "2011-12-14 09:03:59">

irb(main):022:0> new_post.inspect
=> "#<Post id: 2, title: \"just a test\", body: \"body of article test\", user_id: 2, created_at: \"2011-12-14 09:03:59\", updated_at: \"2011-12-14 09:03:59\">"

从我看到的代码是ok的,在转储test.sqlite3模式后,创建的post没有错误

发现了问题。未将用户id定义为数据库中的列。清理数据库并运行rake规范将迁移数据库并修复所有问题。

尝试更改为has\u many:Post,class\u name=>Post,:foreign\u key=>user\u id,:dependent=>destroy and behing\u to:user,:class\u name=>user,:foreign\u key=>user\u id。有时ActiveRecord可能有点不稳定。别忘了重新启动服务器您确定posts表中存在列user_id吗?DB模式是我从rake中获得的。rake DB:Schema:Dump抱歉,我用build编辑了示例以反映生成返回新的POST。这并没有回答他为什么会出现错误的问题。你基本上是在建议不同的方法去做他已经在做的事情。现在作为最后一个建议,你建议做他已经在做的事情。
post = Post.new(posts_attributes)
@user = User.find(1)
@user.posts << post
@user = User.find(1)
@post = @user.posts.create(posts_attributes_as_hash)
User.create(:username => "UserNamedTest", :email => "usernamedtest@somewhere.com")

SQL (13.6ms)  INSERT INTO "users" ("created_at", "email", "password_hash", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Wed, 14 Dec 2011 09:02:46 UTC +00:00], ["email", "usernamedtest@somewhere.com"], ["password_hash", nil], ["password_salt", nil], ["updated_at", Wed, 14 Dec 2011 01:03:26 UTC +00:00], ["username", "UserNamedTest"]]

=> #<User id: 2, username: "UserNamedTest", email: "usernamedtest@somewhere.com", password_hash: nil, password_salt: nil, created_at: "2011-12-14 09:02:46", updated_at: "2011-12-14 09:02:46">

user =  User.find_by_username("UserNamedTest")

User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'UserNamedTest' LIMIT 1

=> #<User id: 2, username: "UserNamedTest", email: "usernamedtest@somewhere.com", password_hash: nil, password_salt: nil, created_at: "2011-12-14 09:02:46", updated_at: "2011-12-14 09:02:46">

new_post = user.posts.create(:title => "just a test", :body =>"body of article test")

SQL (0.5ms)  INSERT INTO "posts" ("body", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["body", "body of article test"], ["created_at", Wed, 14 Dec 2011 09:03:59 UTC +00:00], ["title", "just a test"], ["updated_at", Wed, 14 Dec 2011 09:03:59 UTC +00:00], ["user_id", 2]]

=> #<Post id: 2, title: "just a test", body: "body of article test", user_id: 2, created_at: "2011-12-14 09:03:59", updated_at: "2011-12-14 09:03:59">

irb(main):022:0> new_post.inspect
=> "#<Post id: 2, title: \"just a test\", body: \"body of article test\", user_id: 2, created_at: \"2011-12-14 09:03:59\", updated_at: \"2011-12-14 09:03:59\">"