delegated_type
delegated_type(role, types:, **options)Defines this as a class that’ll delegate its type for the passed role to the class references in types. That’ll create a polymorphic belongs_to relationship to that role, and it’ll add all the delegated type convenience methods:
class Entry < ApplicationRecord delegated_type :entryable, types: %w[ Message Comment ], dependent: :destroy end Entry#entryable_class # => +Message+ or +Comment+ Entry#entryable_name # => "message" or "comment" Entry.messages # => Entry.where(entryable_type: "Message") Entry#message? # => true when entryable_type == "Message" Entry#message # => returns the message record, when entryable_type == "Message", otherwise nil Entry#message_id # => returns entryable_id, when entryable_type == "Message", otherwise nil Entry.comments # => Entry.where(entryable_type: "Comment") Entry#comment? # => true when entryable_type == "Comment" Entry#comment # => returns the comment record, when entryable_type == "Comment", otherwise nil Entry#comment_id # => returns entryable_id, when entryable_type == "Comment", otherwise nil
You can also declare namespaced types:
class Entry < ApplicationRecord delegated_type :entryable, types: %w[ Message Comment Access::NoticeMessage ], dependent: :destroy end Entry.access_notice_messages entry.access_notice_message entry.access_notice_message?
Options
The options are passed directly to the belongs_to call, so this is where you declare dependent etc. The following options can be included to specialize the behavior of the delegated type convenience methods.
- :foreign_key
-
Specify the foreign key used for the convenience methods. By default this is guessed to be the passed role with an “_id” suffix. So a class that defines a delegated_type :entryable, types: %w[ Message Comment ] association will use “entryable_id” as the default :foreign_key.
- :foreign_type
-
Specify the column used to store the associated object’s type. By default this is inferred to be the passed role with a “_type” suffix. A class that defines a delegated_type :entryable, types: %w[ Message Comment ] association will use “entryable_type” as the default :foreign_type.
- :primary_key
-
Specify the method that returns the primary key of associated object used for the convenience methods. By default this is id.
Option examples:
class Entry < ApplicationRecord delegated_type :entryable, types: %w[ Message Comment ], primary_key: :uuid, foreign_key: :entryable_uuid end Entry#message_uuid # => returns entryable_uuid, when entryable_type == "Message", otherwise nil Entry#comment_uuid # => returns entryable_uuid, when entryable_type == "Comment", otherwise nil