开发者

rails 3: how build a string catenating all values of a single field, without iteration?

If my table Foo contains a field :code

If Foo has 3 records with :code = "AAAA" "BBBB" and "CCCC"

I'm trying to build a string

"AAAA_BBBB_CCCC"

(I'm passing a set of field values to an external program via URL and that's how it expects multiple values to be passed to it)

Doing

Foo.select("code").join("_")

doesn't work because the items joined are not the actual value of "code", but some 开发者_如何学JAVAsort of hash or association which has an attribute called "code"


maybe

Foo.select("code").map(&:code).join("_")

but this isn't properly without iteration ...


I thkink you could resolve this with composed_of method instead of simple concatenation http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html http://apidock.com/rails/ActiveRecord/Aggregations/ClassMethods/composed_of

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜