0

특정 열에서만 데이터를 가져 오려고하지만 반환되는 것은 전체 레코드입니다.레일 : 특정 열의 데이터를 가져 옵니까?

나는 두포에서 무슨 일이 일어나고 있는지 보려고 내 콘솔에 갔지만 그 결과는 나를 혼란스럽게합니다.

어떻게하면 각 레코드의 상태 만 반환 할 수 있습니까? MCV에 대한 코드

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 > 

모든 Rails: How to create polymorphic relationship/friendship model

https://github.com/railscasts/154-polymorphic-association-revised/tree/master/blog-after

답변

3

레일 3.1 이상 정확히의 pluck 방법을 소개 나는 라이언 베이츠 다형성 associtions 코드에 기반 한 다음과 같은 질문에 이 company.requests.pluck(:status). 이 함수는 상태를 DB에 쿼리하고 배열을 반환합니다.

이전 버전에서는 사용자 company.requests.select(:status).map(&:status)을 사용할 수 있습니다. 여기에서 각 요청에 대한 모델을 인스턴스화 한 다음 상태를 추출하는 것이 훨씬 비용이 많이 듭니다.

+0

좋은 전화 거는. 그 방법에 대해 몰랐습니다. –

+0

하지만 각 반복마다 .status 메소드로 호출 할 수는 없습니까? –

+0

당신은 콘솔이 실제로 본 company.requests의 전체 블록 인 마지막 오브젝트를 리턴하고있는 것입니다. 각각 대신지도로 변경하면 반환되는 상태 만 표시됩니다. 어떻게 더 잘 작동하는지보고 싶다면 request.status를 줄에 넣고 출력 된 상태를 얻으십시오. –

관련 문제