Search code examples
ruby-on-railsrubyruby-on-rails-4polymorphic-associations

Link objects from polymorphic Associations


I have 2 Models Screen and Album

# Screen
belongs_to :attachable, :polymorphic => true

# Album
has_many :screens, :as => :attachable

I've set up an album select on the Screen Form:

<% album_options = current_user.albums.map { |a| [a.title, "#{a.id}-Album"] } %>
<%= select :screen, :attachable_id, options_for_select(album_options) %>

Now i need to find the Screens who belong to a given album. I've tried a lot variations similar to this:

# Album Controller
@summoner = @album.user
@album_screens = @summoner.screens.where('attachable_id', params[:id])

But somehow every screen is shown in any album.

What am i missing?


Solution

  • Change the where clause to

    where(attachable_id: params[:id])