Rails 5.0 在使用 STI 时不获取多态关联
Rails 5.0 not fetching polymorphic association when STI is used
gem 'rails', '4.2.7.1'
class Property < Accommodation
has_many :attachments, as: :attached_item, dependent: :destroy
end
class Accommodation < ActiveRecord::Base;
end
class Attachment < ActiveRecord::Base
belongs_to :attached_item, polymorphic: true
end
关联在 rails 4.2
中完全正常工作
Property.last.attachments
Property Load (0.9ms) SELECT "accommodations".* FROM "accommodations" WHERE "accommodations"."type" IN ('Property') ORDER BY "accommodations"."id" DESC LIMIT 1
Attachment Load (0.5ms) SELECT "attachments".* FROM "attachments" WHERE "attachments"."attached_item_id" = AND "attachments"."attached_item_type" = [["attached_item_id", 1], ["attached_item_type", "Property"]]
Rails 5.0
中的类似关联
class CustomInquiry < ApplicationRecord;
end
class ChildInquiry < CustomInquiry
has_many :text_histories, as: :mailed_item, dependent: :delete_all
end
class TextHistory < ApplicationRecord
belongs_to :mailed_item, polymorphic: true
end
2.4.0 :002 > ChildInquiry.last.text_histories
ChildInquiry Load (1.2ms) SELECT "custom_inquiries".* FROM "custom_inquiries" WHERE "custom_inquiries"."type" IN ('ChildInquiry') ORDER BY "custom_inquiries"."id" DESC LIMIT [["LIMIT", 1]]
TextHistory Load (0.4ms) SELECT "text_histories".* FROM "text_histories" WHERE "text_histories"."mailed_item_id" = AND "text_histories"."mailed_item_type" = [["mailed_item_id", 197], ["mailed_item_type", "CustomInquiry"]]
但是这里第二个查询应该运行像--
TextHistory Load (0.4ms) SELECT "text_histories".* FROM "text_histories" WHERE "text_histories"."mailed_item_id" = AND "text_histories"."mailed_item_type" = [["mailed_item_id", 197], ["mailed_item_type", "ChildInquiry"]]
任何人都可以帮助我了解 rails 5 中更新的内容以及任何猴子补丁来覆盖它。
最新版本的 ActiveRecord 在关联中使用基数 class,因此当我们使用 STI 时,它使用基数 class 而不是当前的 class。
所以为了解决这个问题,你可以使用 Gem gem "store_base_sti_class"
。
它将在 ActiveRecord 关联中使用当前的 class。
这里是githublink
gem 'rails', '4.2.7.1'
class Property < Accommodation
has_many :attachments, as: :attached_item, dependent: :destroy
end
class Accommodation < ActiveRecord::Base;
end
class Attachment < ActiveRecord::Base
belongs_to :attached_item, polymorphic: true
end
关联在 rails 4.2
中完全正常工作Property.last.attachments
Property Load (0.9ms) SELECT "accommodations".* FROM "accommodations" WHERE "accommodations"."type" IN ('Property') ORDER BY "accommodations"."id" DESC LIMIT 1
Attachment Load (0.5ms) SELECT "attachments".* FROM "attachments" WHERE "attachments"."attached_item_id" = AND "attachments"."attached_item_type" = [["attached_item_id", 1], ["attached_item_type", "Property"]]
Rails 5.0
中的类似关联class CustomInquiry < ApplicationRecord;
end
class ChildInquiry < CustomInquiry
has_many :text_histories, as: :mailed_item, dependent: :delete_all
end
class TextHistory < ApplicationRecord
belongs_to :mailed_item, polymorphic: true
end
2.4.0 :002 > ChildInquiry.last.text_histories
ChildInquiry Load (1.2ms) SELECT "custom_inquiries".* FROM "custom_inquiries" WHERE "custom_inquiries"."type" IN ('ChildInquiry') ORDER BY "custom_inquiries"."id" DESC LIMIT [["LIMIT", 1]]
TextHistory Load (0.4ms) SELECT "text_histories".* FROM "text_histories" WHERE "text_histories"."mailed_item_id" = AND "text_histories"."mailed_item_type" = [["mailed_item_id", 197], ["mailed_item_type", "CustomInquiry"]]
但是这里第二个查询应该运行像--
TextHistory Load (0.4ms) SELECT "text_histories".* FROM "text_histories" WHERE "text_histories"."mailed_item_id" = AND "text_histories"."mailed_item_type" = [["mailed_item_id", 197], ["mailed_item_type", "ChildInquiry"]]
任何人都可以帮助我了解 rails 5 中更新的内容以及任何猴子补丁来覆盖它。
最新版本的 ActiveRecord 在关联中使用基数 class,因此当我们使用 STI 时,它使用基数 class 而不是当前的 class。
所以为了解决这个问题,你可以使用 Gem gem "store_base_sti_class"
。
它将在 ActiveRecord 关联中使用当前的 class。
这里是githublink