Skip to content

fields with false booleans and empty strings seem to cause extra DB hits #4

@jmchambers

Description

@jmchambers

When rendering show for a record the log reveals that, as expected, the controller is hitting the DB with a request for all fields but then, under certain circumstances, the view erb is generating extra requests for individual fields. For example, for a user who hasn't filled in a motto, and who is not an admin, we get:

Processing UserController#show
HBASE SELECT (attribute:name, attribute:motto, attribute:is_admin)
Rendering template within layouts/application
Rendering users/show
HBASE SELECT (attribute:motto)
HBASE SELECT (attribute:is_admin)

I used the console to try and figure out what was going on. I found that when a record is requested (with default = all fields) only some of the fields are actually populated in the returned object e.g.:

>> user = User.find(:first)
=> #< User attribute:name: "jon" >

Subsequent calls to the fields that weren't returned adds them:

>> user.motto
=> nil
>> user
=> #< User attribute:name: "jon", attribute:motto: nil >
>> user.is_admin
=> false
>> user
=> #< User attribute:name: "jon", attribute:motto: nil, attribute:is_admin: false >

I'm not sure if this is expected behaviour or not. Is the DB really being hit multiple times or is that just the way it's reported in the logs? I'm a bit confused!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions