我有這些模型與關聯如下協會在三個模式
class User
has_many :comments
belongs_to :country
end
class Country
has_many :users
has_many :comments
end
class Comment
belong_to :user
belong_to :country
end
評論有user_id
和country_id
作爲列名。 用戶有country_id
作爲列名。
現在,如果我寫這個代碼是控制檯
User.first.comments.create :content=>"some content"
這將創建一個評論,但評論的country_id
列將是空的。我想country_id
基於user_id
充滿我怎樣才能做到這
着它通過協會來完成,無需通過COUNTRY_ID明確 –
@rohankharvi然後你使用回調'before_save'或'before_create'在你的模型@MaximPontyushenko已經可以做到這一點建議。 – dp7