[Rails] HABTM with finder_sql problem (Rails bug?)
Pedro Fayolle
pfayolle at gmail.com
Sun Jan 1 05:48:45 GMT 2006
Thanks a lot for your help, but unfortunately that wasn't the problem.
I did, however, manage to solve it with the use of :conditions instead
of :finder_sql, which also resulted in much cleaner code. I do still
wonder what the problem was.
On 30/12/05, Reginald Braithwaite <raganwald at gmail.com> wrote:
> On 12/30/05, Pedro Fayolle <pfayolle at gmail.com> wrote:
> > I'm building an app that needs i18n support across the entire database
> > (i.e. localized attributes). In order to do this I've created a
> > special HABTM join table that can be associated with _any_ other
> > table:
> >
> > create table language_strings (
> > for_table varchar(255) not null,
> > foreign_id int not null,
> > language_id varchar(5) not null,
> > attr_name varchar(255) not null,
> > value text not null,
> > primary key (for_table, foreign_id, language_id)
> > );
> >
> > Notice the for_table and foreign_id columns. These two are used to
> > identify the table and row to which each record belongs.
> >
> > To make this work I then add something like this to each models that needs it:
> >
> > class Property < ActiveRecord::Base
> > has_and_belongs_to_many :names,
> > :class_name => 'Language',
> > :join_table => 'language_strings',
> > :foreign_key => 'foreign_id',
> > :finder_sql =>
> > "SELECT language_id AS id, value " +
> > "FROM language_strings " +
> > "WHERE for_table = 'properties' AND " +
> > 'foreign_id = #{id} AND ' +
> > "attr_name = 'name'"
> > end
> >
> > ( With pretty highlighting: http://rafb.net/paste/results/n5aV2A60.html )
>
> My WAG (wild-assed guess) is your use of double quites. Even though
> the #{id} section is within single quotes, you've chosen to catenate
> these strings together with "+" and Ruby may be turning the whole
> thing into a string that is resolved early.
>
> I would try it using single quotes and I would also use a multi-line
> string literal rather than catenation. That way you know exactly what
> you are getting:
>
> :finder_sql =>
> 'SELECT language_id AS id, value
> FROM language_strings
> WHERE for_table = \'properties\' AND
> foreign_id = #{id} AND
> attr_name = \'name\''
>
> As a side effect, your code can no longer be confused with Java ;-)
>
> --
> Reginald Braithwaite
> http://www.braithwaite-lee.com/weblog/
>
> If at first you don't succeed, skydiving is not for you.
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
More information about the Rails
mailing list