未定义方法“匹配”当使用 :belongs_to 关联时

发布于 2024-11-27 09:37:40 字数 1210 浏览 0 评论 0原文

我在模型品牌上定义了一个 :belongs_to 关联,如下所示:(

belongs_to  :loyalty_coupon, :class_name => Coupon, :foreign_key => :loyalty_coupon_id

从品牌到优惠券也存在 :has_many 关系,但 :belongs_to 关系旨在挑选出一个特定的优惠券以供特别关注。关系名称和链接字段不同两个关联之间应该没有冲突;我只提及这一点,因为它可以证明相关关系表示为 has_many :couponsbelongs_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-12-04 09:37:40

您的 :class_name 选项应该是字符串,而不是 Class 对象。这样 ActiveRecord 就不会尝试加载可能引用“类 A”的“类 B”,而“类 A”又会引用“类 B”,依此类推。

belongs_to  :loyalty_coupon, :class_name => "Coupon"

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.

belongs_to  :loyalty_coupon, :class_name => "Coupon"

String objects have the match method while Class objects do not.

You don't need to include the :foreign_key option, as it is inferred from the association's name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文