我有以下型号:
class User < ActiveRecord::Base
#method that I want to delete and replace with belongs_to
def restaurant
RemoteRestaurant.find_by_shortRD(self.INFO_SHORT_RD)
end
end
在这种情况下如何添加belongs_to:remote_restaurant?
答案 0 :(得分:0)
使用您的自定义foreign_key和primary_key
添加belongs_toclass User < ActiveRecord::Base
belongs_to :remote_restaurant, :foreign_key => "INFO_SHORT_RD", :primary_key=>"shortRD"
end
答案 1 :(得分:0)
class User < ActiveRecord::Base
belongs_to :restaurant, class_name: 'RemoteRestaurant', foreign_key: :shortRD, primary_key: :INFO_SHORT_KEY
end