我有...
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]