好的,我第一个项目的主要问题是速度。我不知道在内存和数据库调用之间哪个是哪个。我有两个问题:
- 如何区分数据库调用和内存调用(如果它是这样调用的)?
我遇到了一个未定义的方法错误,它指向
`r.status_messages.last.name`
所以我在 Rails 控制台中对其进行了测试(我感觉这与我的第一个问题有关。)
简而言之,- r.status_messages.last.name 不起作用(读取为 2 个命令,r.status_messages.last & .name)
- 但是,h = r.status_messages.last THEN h.name, WORKS
- 但是,当我这样做时,r.status_messages.last.name,它现在可以工作了。
这里发生了什么?
详细信息
request_form has_many status_messages :通过 request_statuses
Rails 3.2.3、Ruby193、Windows 7 Prof、NetBeans IDE 6.9.1
Rails 控制台
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"
编辑:我已经说明了这个问题,关于划分为两个查询。抱歉不清楚。
我输入r.status_messages.last.name
然后按回车
但是 rails 控制台已将其剪切为r.status_messages.last
and .name
。这导致了语法错误。
name
并且description
是状态消息的属性。
但是,当我保存r.status_messages.last
到变量时h
h.name
并h.description
工作。
令人惊讶的是:r.status_messages.last.name
之后也工作得很好。
- 如何区分数据库调用和内存调用(如果它是这样调用的)?
我想要一个很好的参考,其中有一个表格或比较列表,比如
.size
is to.length
。 - 造成这种分裂的原因是什么?