To_s :db

I think I have rails 1.2 installed (>> Successfully installed
rails-1.1.6.5618), but I don’t seem to be able to use to_s(:db)

eg

User.find_all.to_s(:db)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):19:in `to_s’
from (irb):19

I was expecting “1,2,3,4”

I don’t know what the .to_s(:db) is about, but I read that find_all is
deprecated in favor of find(:all). Find all (in either manifestation)
returns a hash whereas .to_s tries to convert some other type of element
into a string.

So what exactly are you trying to do? Being relatively new to Ruby and
Rails myself, I can attest to how arcane the syntax seems at first.

Check out
http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails

and on a more positive note:
http://glu.ttono.us/articles/2006/02/06/rails-best-practices-tips-and-tricks

There are a few very good books about Rails and Ruby that explain the
rationale behind the setup. Search this forum for recommendations,
because different books and tutorials suit different audiences. The
books I’ve found most helpful so far (coming from a desktop db
development environment) are:

Agile Web D. with Rails (2nd edition is now out)
Ruby for Rails

Believe me, it’s worth burying yourself in a good introductory book like
this for a few days (or more…). It’ll save you what the book costs
many many times over in time-wasting frustration.

Shauna

Dano wrote:

User.find_all.to_s(:db)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):19:in `to_s’
from (irb):19

I was expecting “1,2,3,4”

You could implement this in Rails as:
User.find(:all).map(&:id).join(",")

map(&:id) will create an array of id’s for the users, and join(",")
will join them in a string with commas.

If you’re looking to do .to_s(:db) to use this in an IN clause of a
database query, Rails will automatically do that with query bind
parameters:

Finding all the users by their own IDs:

all_user_ids = User.find(:all).map(&:id)
Rails 1.1.6: User.find(:all, :conditions => [“id IN (?)”,
all_user_ids])
Rails 1.2.0: User.find(:all, :conditions => {:id => all_user_ids})

-Dan M.

Dano wrote:

I think I have rails 1.2 installed (>> Successfully installed
rails-1.1.6.5618), but I don’t seem to be able to use to_s(:db)

As far as I know, .to_s(:db) is only available for Time formatting.

Time.now.to_s(:db) #=> “2006-12-29 21:36:20”

See Time::DATE_FORMATS for more.

-Dan M.