好的,我的第一个项目的主要问题是速度。我不知道哪个是内存和数据库之间的调用。我有两个问题:
我遇到了一个指向
的未定义方法错误`r.status_messages.last.name`
所以我在rails控制台上测试了它(我有一种感觉 与我的第一个问题有关。)
简而言之,
细节
request_form has_many status_messages:通过request_statuses
Rails 3.2.3,Ruby193,Windows 7教授,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
和.name
。这导致语法错误。
name
和description
是状态消息的属性。
但是,当我将r.status_messages.last
保存到变量h
h.name
和h.description
工作。
令人惊讶的是:r.status_messages.last.name
之后也工作得很好。
.size
是 .length
。 答案 0 :(得分:0)
:count
将使用sql COUNT查询数据库。您可能想要使用to_a.size
。r.status_messages.last.name
中没有上次状态消息。因此,:name
对nil对象引发了未定义的方法错误。