我在规范中执行unknown attribute: user_id
时遇到错误@user.posts.create
用户类
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
发布课程
class Post < ActiveRecord::Base
attr_accessible :title, :body
belongs_to :user
end
DB Schema
ActiveRecord::Schema.define(:version => 20111214045425) do
create_table "posts", :force => true do |t|
t.string "title"
t.string "body"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "username"
t.string "email"
t.string "password_hash"
t.string "password_salt"
t.datetime "created_at"
t.datetime "updated_at"
end
end
有任何帮助吗?我已经按照我可以找到的使用ActiveRecord的每个指南。我想做的就是创建一个带有相关用户的帖子。
答案 0 :(得分:0)
您可以使用
@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
修改
直接使用create:
@user = User.find(1)
@post = @user.posts.create(posts_attributes_as_hash)
有关详细信息,请查看has_many-association-reference,尤其是4.3.1 Methods Added by has_many
新编辑:
我用你的代码创建了一个新项目,在rails console中我尝试了以下命令
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\">"
从我看到的代码是好的,帖子得到创建没有错误
答案 1 :(得分:0)
在转储test.sqlite3架构后发现了该问题。 user_id
未定义为数据库中的列。吹灭数据库并运行rake spec
迁移数据库并修复所有内容。