Forum: Ruby on Rails `build_association` produces DB-queries

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D585b7d57d57eec701c760d84ab1a79d?d=identicon&s=25 Roman (Guest)
on 2012-07-29 22:48
(Received via mailing list)
Let `User.has_one :profile`. I've always thought that
`@user.build_profile`
is a cheap thing to do - it's just creating a new object without
persisting
it. But, in fact, it does 2 queries to the DB: `BEGIN` and `COMMIT`.

In my app I have an admin users#index page where users' details can be
edited, and to simplify the code I build a profile whenever it's `nil`,
so
I get 2 queries for each user, which increases the page load time by a
factor. So I'd like to know if this is a bug in Rails worth fixing or
that
transaction is really necessary when building an association?

Thanks,
Roman
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-07-29 22:55
(Received via mailing list)
On Saturday, July 28, 2012 6:08:48 PM UTC+1, Roman wrote:
>
> Let `User.has_one :profile`. I've always thought that
> `@user.build_profile` is a cheap thing to do - it's just creating a new
> object without persisting it. But, in fact, it does 2 queries to the DB:
> `BEGIN` and `COMMIT`.
>
>
I've never seen this before. Is there anything interesting about your
profile class ?

Fred
This topic is locked and can not be replied to.