Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 若要覆盖它忽略的属性,请使用exceptarg传递字符串或符号数组: test“注释应该相等”do 断言属性等于( 首先, 第二点, 除了:%i[id在更新时创建\u在编辑时\u在] ) 结束 但在这种情况下,两个实例的标识列均为nil,因为两个实例均未保_Ruby On Rails_Ruby_Activerecord_Identity_Equality - Fatal编程技术网

Ruby on rails 若要覆盖它忽略的属性,请使用exceptarg传递字符串或符号数组: test“注释应该相等”do 断言属性等于( 首先, 第二点, 除了:%i[id在更新时创建\u在编辑时\u在] ) 结束 但在这种情况下,两个实例的标识列均为nil,因为两个实例均未保

Ruby on rails 若要覆盖它忽略的属性,请使用exceptarg传递字符串或符号数组: test“注释应该相等”do 断言属性等于( 首先, 第二点, 除了:%i[id在更新时创建\u在编辑时\u在] ) 结束 但在这种情况下,两个实例的标识列均为nil,因为两个实例均未保,ruby-on-rails,ruby,activerecord,identity,equality,Ruby On Rails,Ruby,Activerecord,Identity,Equality,若要覆盖它忽略的属性,请使用exceptarg传递字符串或符号数组: test“注释应该相等”do 断言属性等于( 首先, 第二点, 除了:%i[id在更新时创建\u在编辑时\u在] ) 结束 但在这种情况下,两个实例的标识列均为nil,因为两个实例均未保存eql?()检查属性的值和类型nil.class==nil.class为true且nil==nil为true,因此OP的第一个示例仍应通过true。你的答案不能解释为什么返回false。它不仅仅是盲目地比较ID,它只在ID有意义时比较ID。正

若要覆盖它忽略的属性,请使用
except
arg传递字符串或符号数组:

test“注释应该相等”do
断言属性等于(
首先,
第二点,
除了:%i[id在更新时创建\u在编辑时\u在]
)
结束

但在这种情况下,两个实例的标识列均为
nil
,因为两个实例均未保存
eql?()
检查属性的值和类型
nil.class==nil.class
true
nil==nil
true
,因此OP的第一个示例仍应通过true。你的答案不能解释为什么返回false。它不仅仅是盲目地比较ID,它只在ID有意义时比较ID。正如Andy Lindeman在回答中提到的:“新记录在定义上不同于任何其他记录”。Rails 3.2.8的更新API文档链接同样值得注意的是,
eql?
被覆盖,但不是别名
equal?
,它仍然比较
object\u id
,这确实是一个比当前标记的正确答案更好的答案。
==
的文档解释了我需要知道的一切要点,以了解rails是如何测试对象平等的。
Failure/Error: Friend.new(name: 'Bob').should eql(Friend.new(name: 'Bob'))

expected #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
     got #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>

(compared using eql?)
Failure/Error: Friend.new(name: 'Bob').should equal(Friend.new(name: 'Bob'))

expected #<Friend:2190028040> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>
     got #<Friend:2190195380> => #<Friend id: nil, event_id: nil, name: 'Bob', created_at: nil, updated_at: nil>

Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
'actual.should == expected' if you don't care about
object identity in this example.
  def self.attributes_to_ignore_when_comparing
    [:id, :created_at, :updated_at]
  end

  def identical?(other)
    self. attributes.except(*self.class.attributes_to_ignore_when_comparing.map(&:to_s)) ==
    other.attributes.except(*self.class.attributes_to_ignore_when_comparing.map(&:to_s))
  end
Address.last.should be_identical(Address.new({city: 'City', country: 'USA'}))
 META = [:id, :created_at, :updated_at, :interacted_at, :confirmed_at]

 def eql_attributes?(original,new)
   original = original.attributes.with_indifferent_access.except(*META)
   new = new.attributes.symbolize_keys.with_indifferent_access.except(*META)
   original == new
 end

 eql_attributes? attrs, attrs2