2014-10-29 42 views
0

我得到了3個與會者,測試用戶和任務,與會者通過多對多關係連接測試用戶&任務。現在我的問題是:爲什麼可以通過「test.ids」檢索所有的id,而不是通過「test.testuser_ids」檢索testuser_ids(如下面的rails console輸出)?從集合中選擇外部值

schema.rb

ActiveRecord::Schema.define(version: 20141027151350) do 

    create_table "attendees", force: true do |t| 
    t.integer "task_id" 
    t.integer "testuser_id" 
    t.boolean "participate" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "tasks", force: true do |t| 
    t.string "title" 
    t.text  "text" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.datetime "task_date" 
    t.boolean "participate" 
    end 

    create_table "testusers", force: true do |t| 
    t.string "username" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end  
end 

軌控制檯:

irb(main):030:0> test = Attendee.all 
    Attendee Load (0.2ms) SELECT "attendees".* FROM "attendees" 
=> #<ActiveRecord::Relation [#<Attendee id: 1, task_id: 1, testuser_id: 1, participate: true, created_at: "2014-10-29 16:29:43", updated_at: "2014-10-29 16:29:43">, #<Attendee id: 2, task_id: 1, testuser_id: 2, participate: true, created_at: "2014-10-29 16:29:43", updated_at: "2014-10-29 16:29:43">, #<Attendee id: 3, task_id: 1, testuser_id: 3, participate: nil, created_at: "2014-10-29 16:29:43", updated_at: "2014-10-29 16:29:43">, #<Attendee id: 4, task_id: 1, testuser_id: 4, participate: nil, created_at: "2014-10-29 16:29:43", updated_at: "2014-10-29 16:29:43">]> 
irb(main):031:0> test.ids 
    (0.2ms) SELECT "attendees"."id" FROM "attendees" 
=> [1, 2, 3, 4] 
irb(main):032:0> test.testuser_ids 
NoMethodError: undefined method `testuser_ids' for #<Attendee::ActiveRecord_Relation:0x00000106011ab0> 

回答

1

是不可能的,因爲默認情況下,Rails不會按照您的要求提供它。 即使我不知道我得到了你想要做什麼,但我認爲這些人是你的解決方案:

Attendee.all.first.testusers.ids 

這將讓來自特定與會者所有testusers IDS。 相反,如果您從所有與會者希望所有testusers ID,您將需要一個循環:

res = [] 
Attendee.all.each { |a| res += a.testusers.ids } 
res.uniq 
+1

對你的答案只是一個簡短的評論。除了'Attendee.all.each {}',你可以簡單地使用'Attendee.pluck(:testuser_id)'。加載與會者的所有字段,然後遍歷所有記錄在這裏是一個很大的開銷。 – blelump 2014-10-29 17:06:17

+0

不錯的建議:)我沒有寫出答案的表現,但是,你的評論肯定是有道理的。 – iMacTia 2014-10-29 17:13:18

+0

非常感謝!工作得很好 - 即使我不明白爲什麼我無法以正常方式訪問測試數組: – 2014-10-30 10:27:15

0

看起來你失蹤協會爲您Attendee模型。它應該有:

class Attendee 

    belongs_to :testuser 

end 

除此之外,你不能叫Attendee.first.testuser_ids因爲attendees表是有testuser_id。否則,改變你的模型模式。

+0

你能解釋這樣對我?我的參加者類已經有belongs_to:testuser&belongs_to:任務集!我沒有得到的是,當我輸入「test = Attendee.all」時,我收到了一系列與會者,但爲什麼可以獲取ID,但無法獲取testuser_ids? - 我對數據庫不太熟悉 - 所以有可能我只是沒有找到正確的東西... – 2014-10-30 10:25:48

+1

好吧,讓我們從頭開始。 'test = Attendee.all'只給所有參加者。現在,如果你要輸入'test.class',你會看到'Attendee :: ActiveRecord_Relation'(我假設你至少使用了Rails 4.0--這裏很重要)。如果你只想得到testuser_id,你可以輸入'Attendee.pluck(:testuser_id)'(這是一種優先選擇)。如果你不喜歡它,也可以鍵入'Attendee.all.collect(&:testuser_id)'(性能更差,因爲你正在獲取完整的記錄,而只需要'testuser_id')。 – blelump 2014-10-30 13:50:32

+1

最後,回答你的問題,爲什麼你不能輸入'Attendee.all.testuser_ids'。你不能,因爲'Attendee.all'返回一個集合(它的類型是'Attendee :: ActiveRecord_Relation'),而不是一個單獨的對象。如果你有一個關聯'has_many testusers',那將是可能的。但是,這裏沒有意義。你應該學習什麼是一個集合,你可以用它做什麼以及什麼是一個單一的對象。這個頁面:我希望,http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html真的很有幫助,並且可以讓你理解它。 – blelump 2014-10-30 13:56:47