रेल के लिए ४
जनरेटर स्तंभ प्रकार को संदर्भ के रूप में स्वीकार करता है (जैसा भी उपलब्ध है belongs_to)।
यह माइग्रेशन एक user_idकॉलम और उपयुक्त इंडेक्स बनाएगा :
$ rails g migration AddUserRefToProducts user:references
उत्पन्न करता है:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, index: true
end
end
http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration
रेल के लिए ३
हेल्पर को संदर्भ (जैसा भी उपलब्ध है belongs_to) कहा जाता है ।
यह माइग्रेशन category_idउपयुक्त प्रकार का एक कॉलम बनाएगा । ध्यान दें कि आप मॉडल नाम पास करते हैं, न कि कॉलम नाम। सक्रिय रिकॉर्ड _idआपके लिए जोड़ता है ।
change_table :products do |t|
t.references :category
end
यदि आपके पास बहुरूपी belongs_toसंघ हैं, तो संदर्भ दोनों स्तंभों को जोड़ देंगे:
change_table :products do |t|
t.references :attachment, :polymorphic => {:default => 'Photo'}
end
attachment_typeएक डिफ़ॉल्ट मान के साथ एक अनुलग्नक_आईडी स्तंभ और एक स्ट्रिंग स्तंभ जोड़ देगा Photo।
http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration