2014-01-06 49 views
-1

我有以下模式:在這種情況下remote_restaurant:的Rails:belongs_to的特定列

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的?

+1

你可以嘗試'belongs_to的:remote_restaurant,foreign_key:「shortRD''?我現在無法測試,我不記得確切的語法。 – zrl3dx

+0

那個INFO_SHORT_RD常數是怎麼回事? – AJcodez

回答

0

添加belongs_to的與您的自定義foreign_key和primary_key

class User < ActiveRecord::Base 
    belongs_to :remote_restaurant, :foreign_key => "INFO_SHORT_RD", :primary_key=>"shortRD" 
end 
0
class User < ActiveRecord::Base 
    belongs_to :restaurant, class_name: 'RemoteRestaurant', foreign_key: :shortRD, primary_key: :INFO_SHORT_KEY 
end