Ask A Question

Notifications

You’re not receiving notifications from this thread.

Finding records using Delegated Types

Neil Tonge asked in Rails

Hi,

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: flickr_collection.id)

-

class Collection < ActiveRecord::Base
    include GalleryItemable
end

-

class Album < ActiveRecord::Base
     include GalleryItemable
end

-

module GalleryItemable
    extend ActiveSupport::Concern

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

-

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
end

I hope someone can help.

Thanks,
Neil

Reply
Join the discussion
Create an account Log in

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

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

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

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

    © 2024 GoRails, LLC. All rights reserved.