我陷入了一個小問題,但對於我試圖弄清楚我做錯了什麼已經很久了。我的場景是我有一個現有的模型user
,現在我創建了另一個名爲`user_comment'的模型。has_many中的關聯,屬於rails中的關聯
用戶模式:
class User < ActiveRecord::Base
has_many :user_comments
end
USER_COMMENT型號:
class UserComment < ActiveRecord::Base
belongs_to :user
end
遷移文件:
class CreateUserComments < ActiveRecord::Migration
def change
create_table :user_comments do |t|
t.integer :user_id
t.string :comments
t.timestamps
end
end
end
運行rake db:migrate
後我去rails console
,然後我在下面提及的細節創建建立兩個表之間的關係,我做了下面的事情NG並沒有什麼工作
obj1= User.first
我第一次加入新行中user_comments表,然後做..
obj2= UserComment.first
做obj1.obj2= obj2
是給我
NoMethodError: undefined method `obj2=' for #<User:0x00000005f8e850>
from /home/insane/.rvm/gems/ruby-2.1.0/gems/activemodel-3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/activerecord-3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
from (irb):3
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
請幫助我如何形成一個協會..