我有以下记录集。我想保存在一个名为@texts的数组中 字段文本的值为每个o a for
1.9.3-p547 :074 > Tweet.all
Tweet Load (0.3ms) SELECT "tweets".* FROM "tweets"
=> [#<Tweet id: 1, text: "hola a todos", zombie_id: 5, created_at: "2014-12-29 23:52:40", updated_at: "2014-12-29 23:52:40">, #<Tweet id: 2, text: "hola como estas", zombie_id: 5, created_at: "2014-12-30 00:09:40", updated_at: "2014-12-30 00:09:40">, #<Tweet id: 3, text: "hello", zombie_id: 5, created_at: "2014-12-30 12:44:41", updated_at: "2014-12-30 12:44:41">]
1.9.3-p547 :075 >
Example
@texts=["hola a todos","hola cmo estas", "hello"];
答案 0 :(得分:2)
我会这样做:
@texts = Tweet.all.map(&:text)
或者:
@texts = Tweet.pluck(:text)
答案 1 :(得分:1)