0

I want pull the data from only a specific column, but what Is returned is the whole record.

I went into my console to try and see what was happening under the hood but the results confuse me.

how can I return just the status for each record?

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 > 

All to code for the MCV is in the following question Rails: How to create polymorphic relationship/friendship model

I have based it on ryan bates polymorphic associtions code https://github.com/railscasts/154-polymorphic-association-revised/tree/master/blog-after

Community
  • 1
  • 1
Aaron Dufall
  • 1,177
  • 10
  • 34

1 Answers1

3

Rails 3.1 or later introduced the pluck method for exactly this company.requests.pluck(:status). This queries the DB for just the statuses and returns an array.

In older versions you can user company.requests.select(:status).map(&:status), here you are instantiating a model for each request and then just pulling out the status, which is a lot more costly.

Bradley Priest
  • 7,438
  • 1
  • 29
  • 33
  • But should't I able to call that as a .status method on each iteration? – Aaron Dufall Jun 30 '12 at 05:54
  • You are, it's just that the console is returning the last object it sees which is actually the whole block of company.requests. If you change it to map instead of each, you will get just the statuses returned. If you'd just like to see how it works better call puts request.status in the line to get the statuses outputted – Bradley Priest Jun 30 '12 at 06:17