我有以下模型(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的样子:
#<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 :(得分: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
仍然愿意接受更好的解决方案......