未定义方法“匹配”当使用 :belongs_to 关联时
我在模型品牌上定义了一个 :belongs_to 关联,如下所示:(
belongs_to :loyalty_coupon, :class_name => Coupon, :foreign_key => :loyalty_coupon_id
从品牌到优惠券也存在 :has_many 关系,但 :belongs_to 关系旨在挑选出一个特定的优惠券以供特别关注。关系名称和链接字段不同两个关联之间应该没有冲突;我只提及这一点,因为它可以证明相关关系表示为 has_many :coupons
和belongs_to :brand
。)
当我显示 Brand.first.loyalty_coupon
时,我得到“nil”(没关系,因为我还没有分配)。当我尝试使用命令Brand.first.loyalty_coupon = Coupon.first
分配忠诚度优惠券时,我收到错误消息:
undefined method `match' for #<Class:0x104036d30>
部分回溯如下:
activerecord (3.0.1) lib/active_record/base.rb:1016:in `method_missing'
activerecord (3.0.1) lib/active_record/base.rb:1180:in `compute_type'
activerecord (3.0.1) lib/active_record/reflection.rb:162:in `send'
activerecord (3.0.1) lib/active_record/reflection.rb:162:in `klass'
activerecord (3.0.1) lib/active_record/reflection.rb:173:in `build_association'
如果我执行Brand,则出现相同的错误消息.first.build_loyalty_coupon
(这并不奇怪,因为它做了同样的事情)。我在优惠券模型中没有 :has_one ,但我也尝试了指定 :class_name 和 :foreign_key ,并且在尝试 Coupon.first.brand_loyalty
时得到了相同的错误和回溯。我想我错过了一些愚蠢的东西,但我看不出它是什么。
I have a :belongs_to association defined on model Brand as follows:
belongs_to :loyalty_coupon, :class_name => Coupon, :foreign_key => :loyalty_coupon_id
(There is also a :has_many relationship from Brand to Coupon, but the :belongs_to relationship is intended to single out one particular coupon for special attention. Relationship names and linking fields are different between the two associations, so there should be no conflict; I only mention this as it could prove relevant. The relationship is expressed as has_many :coupons
and belongs_to :brand
.)
When I display Brand.first.loyalty_coupon
, I get "nil" (that's okay because I haven't assigned one yet). When I attempt to assign a loyalty_coupon with the command Brand.first.loyalty_coupon = Coupon.first
, I get the error message:
undefined method `match' for #<Class:0x104036d30>
with a partial traceback as follows:
activerecord (3.0.1) lib/active_record/base.rb:1016:in `method_missing'
activerecord (3.0.1) lib/active_record/base.rb:1180:in `compute_type'
activerecord (3.0.1) lib/active_record/reflection.rb:162:in `send'
activerecord (3.0.1) lib/active_record/reflection.rb:162:in `klass'
activerecord (3.0.1) lib/active_record/reflection.rb:173:in `build_association'
Same error message if I do Brand.first.build_loyalty_coupon
(not surprising since it's doing the same thing). I had no :has_one in the Coupon model, but I also tried that with :class_name and :foreign_key specified, and got the same error and traceback when I tried Coupon.first.brand_loyalty
. I imagine that I'm missing something stupid, but I can't see what it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
:class_name
选项应该是字符串,而不是 Class 对象。这样 ActiveRecord 就不会尝试加载可能引用“类 A”的“类 B”,而“类 A”又会引用“类 B”,依此类推。String
对象具有match
方法,而Class
对象则没有。您不需要包含
:foreign_key
选项,因为它是从关联名称推断出来的。Your
:class_name
option should be a String, not a Class object. This is so that ActiveRecord does not attempt to load "class B" which may reference "class A" which would reference "class B" and so on and so forth.String
objects have thematch
method whileClass
objects do not.You don't need to include the
:foreign_key
option, as it is inferred from the association's name.