2014-04-01 99 views
0

我正在構建和測試兩種模型的關聯:用戶,帖子。本質上,一個用戶只有一個用戶,但一個用戶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' 
+0

如果返回 「錯誤」,那麼請**,發佈錯誤**。我們仍然猜測可能會出現什麼問題。 – tadman

+2

'user_id'在哪裏大寫爲'User_id'? – vee

+0

您的數據庫模式是否與您的遷移相匹配以進行評論?是否可以編輯遷移並且不重新運行? – Shadwell

回答

1

Post模型缺少user_id的屬性,至少這是你的錯誤意味着什麼。

rails g migration add_user_to_posts user:belongs_to 
rake db:migrate 

你遷移文件顯示你在職位表中的用戶ID,但它可能以dB爲單位丟失,如果你沒有運行遷移。

作爲速戰速決,如果你不在乎安博在DB你的數據,我會跑:

rake db:reset 

或者

rake db:drop db:create db:migrate 
+0

感謝您的想法..我嘗試'rake db:reset',但仍然得到相同的錯誤。我也嘗試過'rails g migration add_user_to_posts..'。這似乎也不起作用。我認爲Post模型可以通過'rails g model來獲得user_id屬性。發表評論:字符串用戶:引用' – funfuntime

+2

@KevinC'rails g模型發表評論:字符串用戶:引用'。它應該是'用戶:參考'不'用戶:參考' – Abednego

+1

@Abednego,很好的發現,昨晚看不到錯誤。你是對的,應該是't.references:user,index:true'。 – rmagnum2002

相關問題