0

我有...

class Report < ActiveRecord::Base
  has_and_belongs_to_many :elements
end

class Element < ActiveRecord::Base
  has_and_belongs_to_many :reports
end

我在命令行看到一些非常奇怪的行为:

$Element.all
[#<Element id: 1, name: "fdafda", created_at: "2013-03-12 02:10:56", updated_at: "2013-03-12 02:10:56">,
 #<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">, 
 #<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]

$ Report.first.elements
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">, 
 #<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">, 
 #<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">, 
 #<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]

(rdb:2) Report.first.elements.uniq
[#<Element id: 2, name: "Foo", created_at: "2013-03-14 10:46:56", updated_at: "2013-03-14 10:46:56">, 
 #<Element id: 3, name: "Bar", created_at: "2013-03-14 10:47:03", updated_at: "2013-03-14 10:47:03">]

元素的重复怎么Report.first.elements可能?我该如何阻止它?

rails -v
Rails 3.2.11
$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.2.1]
4

1 回答 1

0

找到了答案。在报告/id/elements/new 的表单中有一个报告选择器。如果用户在报表选择器中选择了当前报表,则将在连接表中创建元素和报表之间的两个链接。

于 2013-03-14T11:12:56.477 回答