आपके पास गलत माइग्रेशन है - user_id . जोड़ने के बजाय comments . के लिए आप users.commed add जोड़ें .
उफ़। यह हममें से सर्वश्रेष्ठ के साथ हो सकता है।
तो आइए पहले इस गलती को दूर करने के लिए एक माइग्रेशन बनाएं:
class RemoveCommedFromUsers < ActiveRecord::Migration
def change
remove_column :users, :commed # will also remove the index
end
end
बेशक अगर ऐप को तैनात नहीं किया गया है तो आप केवल आपत्तिजनक माइग्रेशन को हटा सकते हैं और rake db:reset चला सकते हैं।
तो चलिए सही माइग्रेशन बनाते हैं
rails g migration AddUserToComments user:belongs_to
जो निम्न माइग्रेशन उत्पन्न करता है:
class AddUserToComments < ActiveRecord::Migration
def change
add_reference :comments, :user, index: true
end
end
add_reference एक स्वीप में एक इंडेक्स और एक विदेशी कुंजी बनाता है।