All threads / Finding records using Delegated Types
Ask A Question


You’re not receiving notifications from this thread.

Finding records using Delegated Types

Neil Tonge asked in Rails


I have a Delegated Types association set up like below and I need to find a particular Collection but the flickr_id is stored in the GalleryItem table. It's failing to return anything. How can I get the following line to work?

collection = Collection.find_by(flickr_id:


class Collection < ActiveRecord::Base
    include GalleryItemable


class Album < ActiveRecord::Base
     include GalleryItemable


module GalleryItemable
    extend ActiveSupport::Concern

    included do
        has_one :gallery_item, as: :gallery_itemable, touch: true, dependent: :destroy


class GalleryItem < ActiveRecord::Base
    belongs_to :collection, optional: true
    belongs_to :album, optional: true
    delegated_type :gallery_itemable, types: %w[ Collection Album ], dependent: :destroy

    acts_as_list scope: :collection

I hope someone can help.


Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 66,029+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.