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:
- How can I differentiate a database call from a memory call (if that's how its called)?
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.
- 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
. - What caused this division?