2011-04-01 44 views
1

我有一個數組我要建這樣的:並使陣列只顯示號碼

Post.where(:user_id => current_user.id, :status_id => 2).select(:id).inspect.to_a 

當我打印可變我得到這個:

[#<Post id: 70>, #<Post id: 44>] 

我想它是:

[70, 44] 

我應該在這裏做什麼?

回答

8

你可以這樣做:

Post.where(:user_id => current_user.id, :status_id => 2).select(:id).map(&:id) 

它還是很好的保持select語句在那裏是可以減少您從數據庫返回的數據量的原因。即使沒有明確的選擇,相同的聲明也能工作,但使用它會更有效率。

+0

確實,你是對的,+1 :) – apneadiving 2011-04-01 19:39:27

1
Post.where(:user_id => current_user.id, :status_id => 2).map {|p| p.id }