【发布时间】:2016-01-16 06:26:38
【问题描述】:
我的模型名为Slider
class Slider < ActiveRecord::Base
end
和HomeBannerSlider与滑块有单表继承关系
class HomeBannerSlider < Slider
has_many :images, as: :imageable
accepts_nested_attributes_for :images, reject_if: :all_blank, allow_destroy: true
end
和Image 给定的模型
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :image
end
我的问题是每当我使用以下命令保存HomeBannerSlider
@admin_home_banner_slider = HomeBannerSlider.new(admin_home_banner_slider_params)
@admin_home_banner_slider.save
它将imageable_type在Image模型中保存为Slider
@admin_home_banner_slider.images
<Image:0x007f5e6ef7af20
id: nil,
imageable_id: nil,
imageable_type: "Slider",
title: "2",
description: "2",
link_button: nil,
link_button_title: nil,
owner: nil,
publish: nil,
priority: nil,
created_at: nil,
updated_at: nil,
image_file_name: nil,
image_content_type: nil,
image_file_size: nil,
image_updated_at: nil>]
但我希望它将imageable_type 存储为HomeBannerSlider
【问题讨论】:
-
滑块表中有类型列吗?
-
是的@TomWalpole ,并且在
type列中它正确存储HomeBannerSlider,但在每个图像实例中它都将Slider存储在imageable_type列中
标签: ruby-on-rails ruby-on-rails-4 polymorphic-associations single-table-inheritance