Method: Array#to_fs

Defined in:
activesupport/lib/active_support/core_ext/array/conversions.rb

#to_fs(format = :default) ⇒ Object Also known as: to_formatted_s

Extends Array#to_s to convert a collection of elements into a comma separated id list if :db argument is given as the format.

This method is aliased to to_formatted_s.

Blog.all.to_fs(:db)  # => "1,2,3"
Blog.none.to_fs(:db) # => "null"
[1,2].to_fs          # => "[1, 2]"


94
95
96
97
98
99
100
101
102
103
104
105
# File 'activesupport/lib/active_support/core_ext/array/conversions.rb', line 94

def to_fs(format = :default)
  case format
  when :db
    if empty?
      "null"
    else
      collect(&:id).join(",")
    end
  else
    to_s
  end
end