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

0 views
Skip to first unread message

isyairazu

unread,
Jul 27, 2010, 11:22:23 PM7/27/10
to Ruby on Rails: Talk
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.

Frederick Cheung

unread,
Jul 28, 2010, 2:34:52 AM7/28/10
to Ruby on Rails: Talk


On Jul 28, 4:22 am, isyairazu <isyair...@gmail.com> 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

isyairazu

unread,
Jul 28, 2010, 7:01:53 AM7/28/10
to Ruby on Rails: Talk
Fred, thank for the reply!

I was able to understand very well.

Thakyou very much!




Fernando Perez

unread,
Jul 28, 2010, 8:25:48 AM7/28/10
to rubyonra...@googlegroups.com

You do understand fixtures are in most cases a bad idea to use.
--
Posted via http://www.ruby-forum.com/.

Marnen Laibow-Koser

unread,
Jul 28, 2010, 8:44:16 AM7/28/10
to rubyonra...@googlegroups.com
Frederick Cheung wrote:
> On Jul 28, 4:22�am, isyairazu <isyair...@gmail.com> 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
mar...@marnen.org

Sent from my iPhone

isyairazu

unread,
Jul 29, 2010, 5:41:59 AM7/29/10
to Ruby on Rails: Talk
Thankyou Fernando and Marnen!
I forget fixtures.
I will learn Machinist.

Thankyou very much!
Reply all
Reply to author
Forward
0 new messages