Rails:belongs_to特定列

时间:2014-01-06 09:56:22

标签: ruby-on-rails ruby orm model relation

我有以下型号:

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?

2 个答案:

答案 0 :(得分:0)

使用您的自定义foreign_key和primary_key

添加belongs_to
class 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