本文介绍了动态的将class_name关系的has_many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与动态属性将class_name关系的has_many

 类分类和LT;的ActiveRecord :: Base的
  的has_many:广告:将class_name => (拉姆达{返回self.item_type})
结束
 

 类分类和LT;的ActiveRecord :: Base的
  的has_many:广告:将class_name => self.item_type
结束
 

但我得到了错误:

 不能转换成PROC字符串
 

 未定义的方法`ITEM_TYPE'的#<类别:0xb62c6c88>
 

修改我有两个不同类型的广告

LeaseAd RentAd 他们实现用单表继承

然后,我有类别广告为嵌套组。我想指定dinamicly哪些类型的广告属于类别对象。

感谢您的帮助!

解决方案

 不能转换成PROC字符串
 

意味着Rails是期待一个字符串

 未定义的方法`ITEM_TYPE'的#<类别:0xb62c6c88>
 

意味着你没有定义 ITEM_TYPE -object

我相信你想在这里什么是不可能这样的。

我会使用类似单表继​​承的广告和它的亚型。

I'm trying to make has_many relation with dynamic class_name attribute

class Category < ActiveRecord::Base
  has_many :ads, :class_name => ( lambda { return self.item_type } ) 
end

or

class Category < ActiveRecord::Base
  has_many :ads, :class_name => self.item_type
end

But i got errors:

can't convert Proc into String

or

undefined method `item_type' for #<Class:0xb62c6c88>

EDITI have two different types of Ads

LeaseAd, RentAd they implemented using single table inheritance

Then i have Category of ads as nested set. I would like to specify dinamicly which type of ads belongs to Category object.

Thank you for any help!

解决方案
can't convert Proc into String

means that rails is expecting a String

undefined method `item_type' for #<Class:0xb62c6c88>

means that you didn't define item_type for the Class-object

I believe what you want here is not possible this way.

I would use something like singletable inheritance for the ads and its subtypes.

这篇关于动态的将class_name关系的has_many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:59