Joins in ActiveRecord#find and least principle surprise

Frequently I lose some time trying to figure out why my tests are failing when I use joins in ActiveRecord#find.

The logic seems fine, but the tests still fail. Than I remember that I should convert the integer properties from columns of other join tables from string to integer:

assert Model.find(:first, :select => 'models.*, other_table.integer_column',    :joins => 'left join other_table on other_table.model_id=models.id').integer_column*.to_i* == 1

What are the reasons that the column types are not the expected ones? Would it be complicated to implement or would it lose performance or just no one cared about this yet?

Thanks in advance,

Rodrigo.

What are the reasons that the column types are not the expected ones? Would it be complicated to implement or would it lose performance or just no one cared about this yet?

It's the same with this case:

Foo.find(:select=> "*, SUM(something) as SomeFakeAttribute")

We don't know the type of those values, and have no reliable way to figure them out. In the event that the database driver itself does typecasting (and some of them do) you'll get the right types automatically.

So it's not a performance thing, we just don't have enough information to reliably do the typecasting. The place that does is the database driver, and that's the place where you'd need to send a patch.

For ruby-mysql and all three ruby postgres drivers (pg, postgres, and postgres-pr), the driver itself returns column values as strings, but it also gives you column type information that you can use to typecast those strings. Sequel uses that column type information to typecast such values correctly. See http://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/postgres.rb#L95 and http://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/mysql.rb#L26 for an examples of conversion procs used based on the column type number.

I'm not saying ActiveRecord should take that approach, I'm just saying the information is there, at least for those drivers.

Jeremy

For ruby-mysql and all three ruby postgres drivers (pg, postgres, and postgres-pr), the driver itself returns column values as strings, but it also gives you column type information that you can use to typecast those strings. Sequel uses that column type information to typecast such values correctly. See http://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/postgres.rb#L95 and http://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/mysql.rb#L26 for an examples of conversion procs used based on the column type number.

I'm not saying ActiveRecord should take that approach, I'm just saying the information is there, at least for those drivers.

I stand corrected!

I believe the oracle / mssql drivers do this typecasting interally and that still feels cleaner, however if someone wanted to take a look at that logic and pull it into the postgres / mysql adapters we can see what breaks / doesn't.

It happens that at this moment I'm very late on a project, but as soon I finish it (in about 3 months), I'll take a look again on this subject.

There is already another patch I would like to send that I am postponning: allow a further option ':full_message' in validations. It will also have to wait...

But at least now I know there is no problem in submitting such patch.

Thank you,

Rodrigo.