On 5 June 2011 10:15, Colin Law <[email protected]> wrote:

> I have never used the for_charity? syntax though.  There does not seem
> much point when it is a boolean.

I've been using it ever since reading dire warnings in the first
edition of Agile Web Development with Rails:

"[To represent true or false, some databases] use integer columns,
where 0 is false and 1 is true... The problem is that in Ruby the
number 0 and the string "f" are both interpreted as true values in
conditions... To query a column as a boolean value in a condition, you
must append a question mark to the column's name"

I must confess I was surprised to learn (prompted by this thread) that
Active Record has a feature that emulates boolean columns for
databases that don't support them natively, so you do get a literal
true or false value out of the attribute, instead of a 1 or 0. And was
even more surprised to learn that this feature was added quite a long
time ago (version 1-ish, AFAICT).

But warnings like the above are still passed on (and in fact are still
present in the current edition of AWDWR). I and others clearly missed
the memo.

> Is that syntax documented somewhere?

It's currently implemented in ActiveRecord::AttributeMethods::Query,
and mentioned in the preamble doc for ActiveRecord::Base (but only as
a way of checking for the presence of attributes, not as a way to
typecast fake boolean columns).

The MySQL connection adaptor's boolean emulation behaviour is
configurable via
ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans, which
is documented.

Chris

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to