我正在構建和測試兩種模型的關聯:用戶,帖子。本質上,一個用戶只有一個用戶,但一個用戶has_many
帖子,但一個帖子belongs_to
。爲什麼沒有.build在rails控制檯中工作?
這就是說,我似乎無法得到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'
如果返回 「錯誤」,那麼請**,發佈錯誤**。我們仍然猜測可能會出現什麼問題。 – tadman
'user_id'在哪裏大寫爲'User_id'? – vee
您的數據庫模式是否與您的遷移相匹配以進行評論?是否可以編輯遷移並且不重新運行? – Shadwell