我有 2 个模型 Screen
和 Album
# Screen
belongs_to :attachable, :polymorphic => true
# Album
has_many :screens, :as => :attachable
我已经在屏幕表单上设置了专辑选择:
<% album_options = current_user.albums.map { |a| [a.title, "#{a.id}-Album"] } %>
<%= select :screen, :attachable_id, options_for_select(album_options) %>
现在我需要找到属于给定专辑的屏幕。我尝试了很多与此类似的变体:
# Album Controller
@summoner = @album.user
@album_screens = @summoner.screens.where('attachable_id', params[:id])
但不知何故,每个屏幕都显示在任何相册中。
我错过了什么?
最佳答案
将where子句改为
where(attachable_id: params[:id])
关于ruby-on-rails - 从多态关联中链接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23314919/