Mini John
Mini John

Reputation: 7941

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?

Upvotes: 1

Views: 50

Answers (1)

Babar
Babar

Reputation: 1202

Change the where clause to

where(attachable_id: params[:id])

Upvotes: 4

Related Questions