我有两种模式:
class Settlement < ActiveRecord::Base
set_primary_key :settlement_identifier
has_many :streets
attr_accessible :city, :name, :service_available, :zip, :country_id,: settlement_identifier
end
class Street < ActiveRecord::Base
belongs_to :settlement, foreign_key: "settlement_identifier"
attr_accessible :name, :settlement_identifier, :street_identifier
end
因为我正在为街道和定居点进口,我需要通过settlement_identifier指向街道,而不是settlement_id。 当我做的时候
Street.first.settlement #it compare settlement_identifiers from both tables
但是当试图从单一定居点获得街道时:
Settlement.first.streets
它会抛出错误
SELECT "streets".* FROM "streets" WHERE "streets"."settlement_id" = 4263
ActiveRecord::StatementInvalid: PG::Error: ERROR: column streets.settlement_id does not exist .
我希望该查询为:
SELECT "streets".* FROM "streets" WHERE "streets"."settlement_identifier" = 4263
任何帮助?
答案 0 :(得分:1)
我解决了这个问题。以下是解决方案:
class CreateSettlements < ActiveRecord::Migration
def change
create_table :settlements, primary_key: :settlement_identifier, id: :false do |t|
t.string :name
t.string :zip
t.string :city
t.string :service_available
t.integer :country_id
t.timestamps
end
end
def down
drop_table :settlements
end
end
这里我在迁移到settlement_identifier时设置了primary_key,并将id设置为false
此外,我的街道迁移是:
class CreateStreets < ActiveRecord::Migration
def change
create_table :streets do |t|
t.string :name
t.integer :settlement_identifier
t.string :street_identifier
t.timestamps
end
end
end
因此,Street通过settlement_identifier引用了Settlement。
结算模式:
class Settlement < ActiveRecord::Base
has_many :streets, foreign_key: "settlement_identifier"
attr_accessible :city, :name, :service_available,:settlement_identifier
end
街头模特:
class Street < ActiveRecord::Base
belongs_to :settlement, foreign_key: "settlement_identifier"
attr_accessible :name, :settlement_identifier, :street_identifier
end
我尝试在Settlement模型上设置primary_key,但这不起作用。 这对我来说很好。如果有人有其他解决方案,请提出评论或代码示例。