# Bugs caused by ActiveRecord::Base#first

**URL:** <https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697>\
**Category:** Rails\
**Created:** [July 21, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697 "2008-07-21T23:38:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 21, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/1 "2008-07-21T23:38:36Z")

</div>

The latest version of Rails introduces the convenience method “first”  
in ActiveRecord, which acts as find(:first). This is breaking my  
application wherever the following type of interaction takes place:

blog.posts.first.title = “New title”  
blog.save

When blog.save is called, an after\_update hook will try to save the  
associated posts. But since the line “blog.posts.first” loads a fresh  
set of posts from the DB – instead of accessing the in-memory cache  
as in previous versions of Rails – the change will not be saved.

The workaround is to say blog.posts[0] instead.

I find this quite insidious, and I’m just curious whether other people  
have encountered the same problem, and do you consider this a poor API  
change on Rails’ part?

Eric

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 21, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/2 "2008-07-21T23:52:06Z")

</div>

i personally would never work that far down an object graph, instead i  
would do

post = blog.posts.first  
post.update\_attribute :title, ‘new title’

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 21, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/3 "2008-07-21T23:53:52Z")

</div>

I would’ve thought #first would’ve been a the #first method from Array.

What do you mean by latest version? Edge or 2.1?

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 22, 2008, 12:42am UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/4 "2008-07-22T00:42:26Z")

</div>

On 21 Jul 2008, at 22:54, Ryan B. wrote:

> I would’ve thought #first would’ve been a the #first method from  
> Array.
> 
> What do you mean by latest version? Edge or 2.1?

2.1 adds this. It’s because of the (usually helpful) feature whereby  
class methods magically exist on associations

(so if you’ve got  
class Post  
def self.foo  
end  
end

then you get blog.posts.foo for free)

I was wondering whether that would cause trouble, although in my mind  
I was mostly worrying about unneeded trips to the database.

Fred

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 22, 2008, 10:18am UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/5 "2008-07-22T10:18:08Z")

</div>

In my case, it did cause association to be loaded when I expect it not  
to. However, I agree I should have never worked that deep in the  
association anyways. I guess it’s just another thing to look out for  
in Rails.

On Jul 21, 4:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [July 22, 2008, 1:01am UTC](https://www.ruby-forum.com/t/bugs-caused-by-activerecord-base-first/143697/6 "2008-07-22T01:01:09Z")

</div>

On 21 Jul 2008, at 23:41, Frederick C. wrote:

> class methods magically exist on associations

I should read the source before I post. foos.first will generate a  
find first in some circumstances.  
In others (most notably if the association is already loaded) it just  
calls first on the already loaded array.  
Still there’s definitely potential for errors there.  
I would have thought (and my experiments seem to confirm) that you’re  
fine with

something.foos.something\_that\_causes\_them\_to\_be\_loaded  
something.foos.first

the opposite will cause problems though

Fred
