Let's imagine an app with several models, and a particular polymorphic 
association:

class Banana < ApplicationRecord
  has_one :eater, as: :eatable
end

class Orange < ApplicationRecord
  has_one :eater, as: :eatable
end

class Eater < ApplicationRecord
  belongs_to :eatable, polymorphic: true
end

When exposed to this codebase, one would often perform a lookup on the code 
for usages of "as: :eatable" or ":as => :eatable" , etc, which can be a 
pain.
If you're like me, you'd like to have some control over what exactly is 
"eatable".

You can perhaps write something like this on "Eater" class:

validates :eatable_type, inclusion: {
                           in: %w[banana orange],
                           message: "you can't eat a %{value}!"
                         }

What about having this validator automatically generated, with a simpler 
syntax? given the following code?

class Eater < ApplicationRecord
  belongs_to :eatable, polymorphic: { 
                         as: %w[banana orange]
                         message: "you can't eat a %{value}!"
                       }
end

The "as" key can be something else,... can't think of a better name now.
Also, we could perhaps allow a more compact version: 

  belongs_to :eatable, polymorphic: %w[banana orange] 

WDYT?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to