假設一個「用戶」想要指定他們最喜歡的「城市」。建立這種關係的正確方法是什麼?Rails:用戶has_many CityPreferences模型關聯
選項1
協會:
User has_many :cities through :city_preferences.
CityPreferences belongs_to :user and :city.
City has_many :users through :city_prefences.
表:
User
email:string, first_name:string, last_name:string
CityPreferences
user:references, city:references
City
name:string
選項2
協會:
個User has_many :city_preferences
CityPreferences belongs_to :user
表:
User
email:string, first_name:string, last_name:string
CityPreferences
user:references city:string
看看'has_many ... through' association:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association – vee
謝謝@vinodadhikary剛更新的問題:) –