我有一张桌子,由于特定原因,我必须使用自定义表格列作为ID:
表格表示具有列ID,我想使用列edavis_id而不是id。
#app/models/representation.rb:
class Representation < ActiveRecord::Base
attr_accessible :people_attributes, :name, :country_id, :rep_type_id, :address, :address_postal, :phone, :email, :fax, :homepage, :consular_district, :botschaftsfunk, :ivs, :ivpn, :idirect
belongs_to :country
belongs_to :rep_type
has_many :people
has_many :representation_telephones, foreign_key: :representation_id
accepts_nested_attributes_for :people
validates_uniqueness_of :remedy_site_id, allow_nil: true
has_many :device_representation_relations
has_many :devices, :through => :device_representation_relations
delegate :name_de, to: :rep_type, prefix: true, allow_nil: true
delegate :iso_code, to: :country, prefix: true, allow_nil: true
# omitted...
end
我如何告诉rails使用edavis_id作为id(而不是常规表id)?
答案 0 :(得分:1)
尝试:
class Representation < ActiveRecord::Base
self.primary_key = :edavis_id
#
end
http://ruby-journal.com/how-to-override-default-primary-key-id-in-rails/
答案 1 :(得分:0)
只需添加:
class Representation < ActiveRecord::Base
self.primary_id = :edavis_id
#...
end