Rails3 has_and_belongs_to_many - 从连接表获取属性时出错

发布于 2024-12-05 10:32:13 字数 883 浏览 1 评论 0原文

鉴于我有以下结构:

class A < ActiveRecord::Base
  has_and_belongs_to_many :bs, :class_name => "B", :join_table => "ab"
end

class AB < ActiveRecord::Base
  #ab has a date column "creation_date"
  #ab also has a text column "creatior"
end

class B < ActiveRecord::Base
end

成功检索“creation_date”属性,

console> A.find(1).bs.first.creation_date
        => "14/08/1874"

我在控制器中

@a = A.find(1)
@bs = a.bs

但在视图(部分)中使用它,我收到以下错误

bs.each do |b|
  b.b_attribute #this is O.K.
  b.creation_date # cause error =>    undefined method `creation_date` for #<B:...>
end

 # also try to debug in partial
 A.find(1).bs.first.creation_date #=> this return data correctly

问题如上所示,可能会导致什么未定义的方法,而直接属性仍然可以访问。

有人知道我的代码有什么问题吗?

Given that I have following structure:

class A < ActiveRecord::Base
  has_and_belongs_to_many :bs, :class_name => "B", :join_table => "ab"
end

class AB < ActiveRecord::Base
  #ab has a date column "creation_date"
  #ab also has a text column "creatior"
end

class B < ActiveRecord::Base
end

I successfully retrieve "creation_date" attribute in rails console

console> A.find(1).bs.first.creation_date
        => "14/08/1874"

In controller

@a = A.find(1)
@bs = a.bs

But using it in a view (partial), I got following error

bs.each do |b|
  b.b_attribute #this is O.K.
  b.creation_date # cause error =>    undefined method `creation_date` for #<B:...>
end

 # also try to debug in partial
 A.find(1).bs.first.creation_date #=> this return data correctly

The problem as shown above, what can possibly cause undefined method whilst the direct attributes are still accessible.

Anyone have any idea what is wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

污味仙女 2024-12-12 10:32:13

当您处理中间模型时,不应使用 has_and_belongs_to_many。该方法仅在连接表不是模型时才有效。您需要明确区分模型的概念。在 Rails 中,您很少能够访问底层表,大多数情况下您会处理包装模型。

如果您的连接表除了 AB 的外键之外还有其他内容,并且您需要访问这些附加数据,那么它需要是一个模型。就您而言,确实如此,但您没有使用正确的关系。它应该看起来像这样:

class A < ActiveRecord::Base
  has_many :abs
  has_many :bs, :through => :abs
end

class AB < ActiveRecord::Base
end

class B < ActiveRecord::Base
end

然后,访问 creation_datecreator 应该通过 AB 模型完成,因为它确实是属于它。

请查看此处的示例快速解释:http://guides.rubyonrails.org/association_basics.html#choosing- Between-has_many-through-and-has_and_belongs_to_many

When you're dealing with an intermediate model, you shouldn't use has_and_belongs_to_many. That method works only when the join table is not a model. You need to make a clear distinction between the concepts of a model and a table. In rails, you rarely have access to the underlying table, most often you deal with the wrapping models.

If your join table has anything more than the foreign keys to A and B, and you need to access that additional data, then it needs to be a model. In your case, it is, but you're not using the correct relation. It should look like this:

class A < ActiveRecord::Base
  has_many :abs
  has_many :bs, :through => :abs
end

class AB < ActiveRecord::Base
end

class B < ActiveRecord::Base
end

Afterwards, accessing creation_date and creator should be done through the AB model, since it really is an attribute that belongs to it.

Take a look here for a quick explanation with examples: http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

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