2012-09-17 186 views
0

我有以下型號(venmas是舊的數據庫,所以我不能改變它):活動記錄外鍵關聯軌

models/venmas.rb 
class Venmas < ActiveRecord::Base 
    establish_connection "turnkey" 
end 

models/yard.rb 
class Yard < ActiveRecord::Base 
end 

我希望能夠做到:

Yard.first.venmas.first # to get the first record of venmas for that yard 

這是院子裏的樣子:

#<Yard id: 1, name: "nyssa", created_at: "2012-08-07 16:58:02", updated_at: "2012-08-07 16:58:02"> 

和Venmas:

#<Venmas VENDOR_NUMBER: " ", ... , yard: "nyssa"> # there are a lot of columns in this one and they aren't important. 

另外一個活動扳手我這是inflections.rb:

ActiveSupport::Inflector.inflections do |inflect| 
    inflect.irregular 'venmas', 'venmas' 
end 

我可以刪除這一點,如果它可以更容易,但我這樣做的所有遺留表,因爲我剛剛從他們讀。

我相信這可以通過分配foreign_key來完成,但我不確定將這些關聯的正確方法嗎?

請讓我知道是否有任何我缺少的東西。

回答

0

想通了

models/venmas.rb 
class Venmas < ActiveRecord::Base 
    establish_connection "turnkey" 
    belongs_to :yard, foreign_key: :yard, primary_key: :name 
end 

models/yard.rb 
class Yard < ActiveRecord::Base 
    has_many :venmas, foreign_key: :yard, primary_key: :name 
end 

仍然是開放的更好的解決方案...