我正在使用两种模式构建和测试关联:用户,帖子。基本上,用户has_many
帖子,但帖子belongs_to
只有一个用户。
那就是说,我似乎无法让User.first.posts.build
工作。我一直在回错。
**作为一个附带问题,为什么User_id
在Post模型中大写?在我见过的大多数例子中,情况并非如此。如果没有大写,它会运行此错误:
Post.create(comment: "yolo molo tolo", user_id: 1)
ActiveRecord::UnknownAttributeError: unknown attribute: user_id
CODE
(1)user.rb
class User < ActiveRecord::Base
validates :username, presence: true, length: { minimum: 6, maximum: 40}
has_many :posts
end
(1)create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.timestamps
end
end
end
(3)Post.rb
class Post < ActiveRecord::Base
belongs_to :User
validates :comment, presence: true, length: { minimum: 5, maximum: 30 }
end
(4)create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :comment
t.references :User, index: true
t.timestamps
end
end
end
错误
p3 = User.first.posts.new
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
ActiveRecord::UnknownAttributeError: unknown attribute: user_id
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/attribute_assignment.rb:47:in `rescue in _assign_attribute'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/attribute_assignment.rb:23:in `each'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/associations/association.rb:178:in `initialize_attributes'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/associations/association.rb:251:in `block in build_record'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/core.rb:187:in `initialize'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/inheritance.rb:27:in `new'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/inheritance.rb:27:in `new'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/reflection.rb:189:in `build_association'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/associations/association.rb:250:in `build_record'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:114:in `build'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.3/lib/active_record/associations/collection_proxy.rb:229:in `build'
from (irb):19
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/commands/console.rb:90:in `start'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/commands/console.rb:9:in `start'
from /Users/MightyMouse/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
答案 0 :(得分:1)
帖子模型缺少user_id属性,至少这是你的错误意味着什么。
rails g migration add_user_to_posts user:belongs_to
rake db:migrate
您的迁移文件显示您在posts表中有用户ID,但如果您没有运行迁移,则db中可能会丢失它。
作为快速解决方案,如果您不关心数据库中的数据,我会运行:
rake db:reset
或者
rake db:drop db:create db:migrate