How to get all model attributes minus several

So, I am writing an rspec test. It will check if the model is duplicated correctly. So the specification looks something like this:

it "should copy the data" do @model = build(:model) @another_model.copy_data(@model) @model.data.should == @another_model.data end 

Data is an embedded document, so it is duplicated when I do this. All model attributes are copied successfully minus id and date created_at. Is there a way to do something like this?

  @model.data.attributes.without(:_id, :created_at).should == @another_model.data.attributes.without(:_id, :created_at) 

Or vice versa, where do I select all other fields without id and created_at?

Thanks!

+6
source share
2 answers

It works

 @model.attributes.except("id", "created_at").should == @another_model.attributes.except("id", "created_at") 
+20
source

You can do something like this, since .attributes returns a hash, where each pair of keys and values ​​is an attribute along with its value.

 @model.data.attributes.each do |k,v| next if k == 'id' || k == 'created_at' # Skip if the key is id or created_at v.should == @another_model.data.attributes[k] end 
0
source

Source: https://habr.com/ru/post/955125/


All Articles