开发者

How do I handle a simple polymorphic association in my views?

开发者 https://www.devze.com 2023-03-12 19:39 出处:网络
I\'ve got some models that look like this: class Basket has_many :fruits, :dependent => :destroy end class Fruit

I've got some models that look like this:

class Basket
  has_many :fruits, :dependent => :destroy
end

class Fruit
  belongs_to :basket  # do I need a polymorphic association here?
end

class Apple < Fruit
  validate :crunchy
end

class Banana < Frui开发者_开发百科t
  validate :peelable
end

Fruit is abstract in the sense that you never create, update, etc., Fruits, but rather Apples or Bananas. That means that I can't write something like edit_fruit_path(@fruit) in my views and have it automatically resolve.

What should I write in my views so that it always resolves to edit_apple_path(@fruit) or edit_banana_path(@fruit)?


This is not a polymorphic but rather a Single Table Inheritance.

I am assuming that you will inherit ActiveRecord::Base to Fruit.

Add the column type to your fruits table.

Now you can do edit_fruit_path(@apple) and it will be the Apple object.

0

精彩评论

暂无评论...
验证码 换一张
取 消