STI 多态 has_many 使用了错误的类型值
STI polymorphic has_many uses wrong type value
我有以下 STI 模型,它们具有多态关联,其查询构造错误
class Product < ApplicationRecord
has_many :images, as: :imageable
end
class OneProduct < Product
end
class Image < ApplicationRecord
belongs_to :imageable
end
在 rails 控制台中,当我
> OneProduct.last.icon_images
正在触发的查询是
SELECT * FROM images WHERE imageable_id = id AND imageable_type = 'Product'
我期待:
SELECT * from images WHERE imageable_id = id AND imageable_type = 'OneProduct'
我是不是有什么期待?
补充信息:数据库是 postgres。
来自 Rails 文档:
Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order for the associations to work as expected, ensure that you store the base model for the STI models in the type column of the polymorphic association. To continue with the asset example above, suppose there are guest posts and member posts that use the posts table for STI. In this case, there must be a type column in the posts table.
Note: The attachable_type= method
is being called when assigning an
attachable. The class_name
of the attachable is passed as a String
.
class Asset < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
def attachable_type=(class_name)
super(class_name.constantize.base_class.to_s)
end
end
class Post < ActiveRecord::Base
# because we store "Post" in attachable_type now dependent: :destroy will work
has_many :assets,as: :attachable, dependent: :destroy
end
class GuestPost < Post end
class MemberPost < Post end
所以它说,而不是存储 imageable_type = OneProduct
你只需要将它存储为 Product
并且你可以在 Product
table。但这完全取决于您对 OneProduct
模型的需求,如果该模型上的 default_scope
可以在不添加 type
列的情况下为您工作,那么请不要将其添加到Product
table 如果这不起作用,那么您可以添加该列,然后添加 default_scope
以在查询 OneProduct
模型时获取 products.type = OneProduct
。
我有以下 STI 模型,它们具有多态关联,其查询构造错误
class Product < ApplicationRecord
has_many :images, as: :imageable
end
class OneProduct < Product
end
class Image < ApplicationRecord
belongs_to :imageable
end
在 rails 控制台中,当我
> OneProduct.last.icon_images
正在触发的查询是
SELECT * FROM images WHERE imageable_id = id AND imageable_type = 'Product'
我期待:
SELECT * from images WHERE imageable_id = id AND imageable_type = 'OneProduct'
我是不是有什么期待?
补充信息:数据库是 postgres。
来自 Rails 文档:
Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order for the associations to work as expected, ensure that you store the base model for the STI models in the type column of the polymorphic association. To continue with the asset example above, suppose there are guest posts and member posts that use the posts table for STI. In this case, there must be a type column in the posts table.
Note: The
attachable_type= method
is being called when assigning an attachable. Theclass_name
of the attachable is passed as aString
.class Asset < ActiveRecord::Base belongs_to :attachable, polymorphic: true def attachable_type=(class_name) super(class_name.constantize.base_class.to_s) end end class Post < ActiveRecord::Base # because we store "Post" in attachable_type now dependent: :destroy will work has_many :assets,as: :attachable, dependent: :destroy end class GuestPost < Post end class MemberPost < Post end
所以它说,而不是存储 imageable_type = OneProduct
你只需要将它存储为 Product
并且你可以在 Product
table。但这完全取决于您对 OneProduct
模型的需求,如果该模型上的 default_scope
可以在不添加 type
列的情况下为您工作,那么请不要将其添加到Product
table 如果这不起作用,那么您可以添加该列,然后添加 default_scope
以在查询 OneProduct
模型时获取 products.type = OneProduct
。