0

Ok, my main problem on my first project was speed. I don't know which is which, between memory and database calls. I have two questions:

  1. How can I differentiate a database call from a memory call (if that's how its called)?
  2. I had encountered an undefined method error that points to

    `r.status_messages.last.name`
    

    So I tested it in the rails console (I had a feeling this is related on my first question.)

    In short,
    • r.status_messages.last.name doesn't work (read as 2 commands, r.status_messages.last & .name)
    • BUT, h = r.status_messages.last THEN h.name, WORKS
    • HOWEVER when I do, r.status_messages.last.name, IT WORKS now.
      What happened here?

details
request_form has_many status_messages :through request_statuses
Rails 3.2.3, Ruby193, Windows 7 Prof, NetBeans IDE 6.9.1
Rails console

r = RequestForm.find(25) 
r = RequestForm.find(25)
  [1m[35mRequestForm Load (1.0ms)[0m  SELECT `request_forms`.* FROM `request_forms` WHERE `request_forms`.`id` = ? LIMIT 1  [["id", 25]]
#<RequestForm id: 25, control_no: 1, requested: true, cds_requested: nil, cds_returned: nil, approval: false, cds_released: nil, cds_recycled: nil, defective_cds: 0, request_date: "2012-05-30", released_date: nil, user_id: 3, admin_id: nil, remarks: nil, created_at: "2012-05-30 07:13:50", updated_at: "2012-05-30 07:13:50">
r.status_messages
r.status_messages
[#<StatusMessage id: 1, description: "can be edited until sent", user_id: nil, created_at: "2012-05-30 07:31:02", updated_at: "2012-05-30 07:31:02", name: "Created">, #<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">]
  [1m[36mStatusMessage Load (1.0ms)[0m  [1mSELECT `status_messages`.* FROM `status_messages` INNER JOIN `request_statuses` ON `status_messages`.`id` = `request_statuses`.`status_message_id` WHERE `request_statuses`.`request_form_id` = 25[0m
r.status_messages.last
r.status_messages.last
#<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
r.status_messages.last
.name
r.status_messages.last
#<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
.name
SyntaxError: (irb):6: syntax error, unexpected '.'
.name
 ^
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
r.status_messages.last
.description
r.status_messages.last
#<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">
.description
SyntaxError: (irb):10: syntax error, unexpected '.'
.description
 ^
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
h = r.status_messages.last

h = r.status_messages.last
#<StatusMessage id: 2, description: "visible to admin", user_id: nil, created_at: "2012-05-30 07:32:51", updated_at: "2012-05-30 07:32:51", name: "Sent">

h.name
h.name
"Sent"
r.status_messages.last.name
r.status_messages.last.name
"Sent"
r.status_messages.last.description
r.status_messages.last.description
"visible to admin"

EDIT: I have stated that problem, regarding division to two queries. Sorry for not being clear.

I typed r.status_messages.last.name then pressed on enter

but rails console had cut it to r.status_messages.last and .name. Which caused the syntax error.

name and description are attributes of status message.

However, when I saved r.status_messages.last to a variable h

h.name and h.description worked.

And amazingly: r.status_messages.last.name worked fine, too, afterwards.

  1. How can I differentiate a database call from a memory call (if that's how its called)? I want a good reference, where there's a table or list of comparisons, like .size is to .length.
  2. What caused this division?
  • Please take a good look at your code yourself. I bet you too will see the syntax error messages. – rubish May 30 '12 at 09:45
  • It's not clear to me what all that console output is, but in the error cases, like ".description", it appears the ".description" is on its own line, which is indeed a syntax error, as indicated. – Dave Newton May 30 '12 at 09:45
  • Thanks @rubish , the syntax would, of course, be wrong, after rails console has cut it into two. Any idea what caused this? – Rafael Perea May 31 '12 at 01:31
  • @RafaelPerea No clue; no rails console I've ever used randomly chops up user input--if this is a NetBeans console window your answer likely lies there. – Dave Newton May 31 '12 at 01:38
  • Oh, so it seems its more of a netbeans issue than rails, Any reference that answers my first question? @DaveNewton, I want a list of comparisons, like, `.size` is to `.length` – Rafael Perea Jun 01 '12 at 08:18

1 Answers1

0
  1. Lazy loading was confusing to me that time. That was why my app had unexpected database calls. Carelessly calling :count will query the database with an sql COUNT. you might want to use to_a.size.
  2. It was a nilClass exception. There was no last status message in r.status_messages.last.name. Hence, :name to a nil object threw an undefined method error.
  3. The Netbeans terminal seems to be the problem here, as commented by @DaveNewton.