Db:fixures:load does not call Model.save()

Hello,

$ ruby script/generate model balance plus:integer minus:integer
equal:integer

$ cat app/models/balance.rb
class Balance < ActiveRecord::Base
def save
self.equal = self.plus - self.minus
super
end
end

$ cat test/fixtures/balances.yml
_10-1:
plus: 10
minus: 1

$ rake db:fixtures:load

$ sqlite3 db/development.sqlite3
sqlite> select * from balances;
//=> |xxx|10|1||…
// equal is not 9

Even create_or_update() was the same.

Should I write it as
equal: 9
in the fixture-file?

It’s not DRY.

Thanks.

On Jul 28, 4:22 am, isyairazu [email protected] wrote:

$ sqlite3 db/development.sqlite3
sqlite> select * from balances;
//=> |xxx|10|1||…
// equal is not 9

Even create_or_update() was the same.

fixtures are directly inserted into the db

Should I write it as
equal: 9
in the fixture-file?

It’s not DRY.

It isn’t, but then again your data just isn’t dry. You can use erb in
your fixtures ( <%= %>) if it’s bothering you that much.

Fred

Fred, thank for the reply!

I was able to understand very well.

Thakyou very much!

isyairazu wrote:

Fred, thank for the reply!

I was able to understand very well.

Thakyou very much!

You do understand fixtures are in most cases a bad idea to use.

Thankyou Fernando and Marnen!
I forget fixtures.
I will learn Machinist.

Thankyou very much!

Frederick C. wrote:

On Jul 28, 4:22�am, isyairazu [email protected] wrote:

$ sqlite3 db/development.sqlite3
sqlite> select * from balances;
//=> |xxx|10|1||…
// equal is not 9

Even create_or_update() was the same.

fixtures are directly inserted into the db

Which is one of the many reasons they should never, ever, ever be used.
The core team completely screwed up the feature. Just forget that
fixtures exist and use factories (I like Machinist for this). Your
testing will be much easier.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone