I was just trying to write a "smart" copy method in a class* and
noticed that the last and count collection methods don't work the same
after build and before save. In other words, if in my code I do this:

    user = User.new
    user.save
    purchase = user.purchases.build

Then I get the following:
    user.purchases.count => 0
    user.purchases.last => #<Purchase:0x01234567>

It's never bothered me before now. As a human, I can ask the question
how can the count be 0 if the collection has a last element? I would
expect the last method to return nil in this case because I haven't
saved purchase yet.

I'm assuming the solution is to create my own "last" method, one that
filters out records with a nil value for created_at. Or another build
method that calls last before build. But both solutions seem un-DRY,
or worse un-DRRoR. But I thought maybe I am missing something.

*In case you're wondering, the method I'm writing copies certain
values of the last "purchase" model created by that user into a new
one, as a way of setting some default values. Maybe there is a more
rails-ee way of doing that too.

Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to