Rails:从特定列获取数据?

时间:2012-06-30 03:18:17

标签: ruby-on-rails activerecord codeblocks polymorphic-associations

我想只从特定列中提取数据,但返回的是整个记录。

我走进我的控制台,试着看看幕后发生了什么,但结果让我感到困惑。

如何只返回每条记录的状态?

1.9.3p194 :001 > company = Company.first
Company Load (0.1ms)  SELECT "companies".* FROM "companies" LIMIT 1
=> #<Company id: 1, name: "Supreme WIndows", created_at: "2012-06-24 04:12:02", updated_at: "2012-06-24 04:12:02"> 
1.9.3p194 :002 > company.requests
Request Load (0.1ms)  SELECT "requests".* FROM "requests" WHERE "requests"."requestable_id"    = 1 AND "requests"."requestable_type" = 'Company'
=> [#<Request id: 1, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:25:11", updated_at: "2012-06-30 01:25:11", profile_id: nil>, #
     <Request id: 2, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:28:16", updated_at: "2012-06-30 01:28:16", profile_id: nil>, #
     <Request id: 3, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:32:55", updated_at: "2012-06-30 01:32:55", profile_id: nil>, #
     <Request id: 4, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 02:46:07", updated_at: "2012-06-30 02:46:07", profile_id: nil>] 
1.9.3p194 :003 > company.requests.status
NoMethodError:   Request Load (0.2ms)  SELECT "requests".* FROM "requests" WHERE "requests"."requestable_id" = 1 AND "requests"."requestable_type" = 'Company'
undefined method `status' for #<ActiveRecord::Relation:0x007fdbd57e1ba8>
from /Users/Aaron/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation/delegation.rb:45:in `method_missing'
from /Users/Aaron/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
from (irb):3
from /Users/Aaron/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/Aaron/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/Aaron/.rvm/gems/ruby-1.9.3-p194/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>'
1.9.3p194 :004 > company.requests.first.status
=> nil 
1.9.3p194 :005 > company.requests.each do |request|
1.9.3p194 :006 >     request.status
1.9.3p194 :007?>   end
=> [#<Request id: 1, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:25:11", updated_at: "2012-06-30 01:25:11", profile_id: nil>, # 
     <Request id: 2, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:28:16", updated_at: "2012-06-30 01:28:16", profile_id: nil>, #
     <Request id: 3, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 01:32:55", updated_at: "2012-06-30 01:32:55", profile_id: nil>, #
     <Request id: 4, status: nil, requestable_id: 1, requestable_type: "Company", created_at: "2012-06-30 02:46:07", updated_at: "2012-06-30 02:46:07", profile_id: nil>] 
1.9.3p194 :008 > 

所有要编码的MCV都在以下问题中 Rails: How to create polymorphic relationship/friendship model

我基于ryan bates多态关联代码 https://github.com/railscasts/154-polymorphic-association-revised/tree/master/blog-after

1 个答案:

答案 0 :(得分:3)

Rails 3.1或更高版本为此pluck引入了company.requests.pluck(:status)方法。这会向DB查询状态并返回一个数组。

在旧版本中,您可以使用company.requests.select(:status).map(&:status),在这里您要为每个请求实例化一个模型,然后只是提取状态,这会花费更多。