我有多态模型:
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Service < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
为了让我的 activeadmin 模型与父母双方(服务和产品)一起工作,我需要执行以下操作:
ActiveAdmin.register Picture do
def who_do_i_belong_to?
uri = how_to_get_uri?
if uri.match(/products/)
:product
else
:service
end
end
belongs_to who_do_i_belong_to?
end
解决方法似乎有效。我只是想念如何从 who_do_i_belong_to 中获取 url/uri?方法。
controller.controller_name # "admin/services", so it is not useful.
提前谢谢你。