Globalize doesn''t work: HELP ME PLEASE...

Gianluca Tessarolo
Thu, 09 Aug 2007 19:17:42 -0000

Hi to all,

seems that Globalize creates wrong sql statements on dynamic finders on
translated columns (find_by_{attribute})  when locale is not en-US.

Globalize Plugin (for_1_2) installed on vendor/plugins under application
root
Rails 1.2.3
Ruby 1.8.6
Mysql  Ver 14.12 Distrib 5.0.37, for redhat-linux-gnu (i686) using
readline 5.0
Fedora Core 7

Example:

Migration / Table:

db/migrate/001_test1.rb
class Test1 < ActiveRecord::Migration
  def self.up
    create_table(:test1s, :options => ''DEFAULT
CHARSET=UTF8'') do |t|
      t.column :name, :string, :limit => 100, :null => false
      t.column :description, :string, :limit => 100, :null => false
      t.column :created_at, :datetime, :null => false
      t.column :updated_at, :datetime, :null => false
    end
  end
  def self.down
    drop_table :test1s
  end
end

Model:
app/models/test1.rb
class Test1 < ActiveRecord::Base
  translates :description
  validates_presence_of :name, :description
  validates_uniqueness_of :name, :description
end

Configuration:
config/environment.rb
... on last line...
Globalize::Locale.set_base_language "en-US"

Run rake db:migrate

Run script/console

t=Test1.new
=> #<Test1:0xb76b1bf8 @attributes={"name"=>"",
"updated_at"=>nil,
"description"=>"", "created_at"=>nil},
@new_record=true>
t.name="n1"
=> "n1"
t.description="d1"
=> "d1"
t.save!
=> true

Globalize::Locale.set "en-US"
=> #<Globalize::Locale:0xb769db08 @currency_decimal_sep=".",
@currency_format=nil, @decimal_sep=".", @language=English,
@code="en-US", @date_format=nil, @thousands_sep=",",
@country=#<Globalize::Country:0xb76999e0
@attributes={"date_format"=>nil,
"currency_decimal_sep"=>".",
"thousands_sep"=>",",
"code"=>"US",
"number_grouping_scheme"=>"western",
"english_name"=>"United States of America",
"decimal_sep"=>".",
"id"=>"223",
"currency_code"=>"USD",
"currency_format"=>nil}>,
@number_grouping_scheme=:western, @currency_code="USD">

Test1.find_by_description("d1")
=> #<Test1:0xb76943b4 @original_language=English,
@attributes={"name"=>"n1",
"updated_at"=>"2007-08-09 11:24:49",
"id"=>"3", "description"=>"d1",
"created_at"=>"2007-08-09 11:24:49"}>

# All works only because the locale is en-US...

Globalize::Locale.set "it-IT"
=> #<Globalize::Locale:0xb769271c @currency_decimal_sep=",",
@currency_format="EURO %n", @decimal_sep=",",
@language=Italian,
@code="it-IT", @date_format=nil, @thousands_sep=".",
@country=#<Globalize::Country:0xb768ff08
@attributes={"date_format"=>nil,
"currency_decimal_sep"=>",",
"thousands_sep"=>".",
"code"=>"IT",
"number_grouping_scheme"=>"western",
"english_name"=>"Italy",
"decimal_sep"=>",", "id"=>"106",
"currency_code"=>"EUR",
"currency_format"=>"EURO %n"}>,
@number_grouping_scheme=:western, @currency_code="EUR">

Test1.find_by_description("d1")
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ''COALESCE(t_description.text,
test1s.description)  = ''d1'')  LIMIT 1'' at line 1:
SELECT test1s.id,
test1s.name, test1s.created_at, test1s.updated_at,
COALESCE(t_description.text, test1s.description) AS description,
t_description.text AS description_not_base  FROM test1s   LEFT OUTER
JOIN globalize_translations AS t_description ON t_description.table_name
= ''test1s'' AND test1s.id = t_description.item_id AND
t_description.facet
= ''description'' AND t_description.language_id = 2600  WHERE
(test1s.
COALESCE(t_description.text, test1s.description)  = ''d1'')
LIMIT 1
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in

`log''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:243:in

`execute''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:399:in

`select''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in

`select_all''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:427:in

`find_by_sql''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:997:in

`globalize_old_find_every''
        from
./script/../config/../config/../vendor/plugins/for-1.2/lib/globalize/localization/db_translate.rb:885:in

`find_every''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:991:in

`find_initial''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1204:in

`send''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1204:in

`method_missing''
        from
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/deprecation.rb:44:in

`silence''
        from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1204:in

`method_missing''
        from (irb):9

# And now this not works because the locale is "it-IT" WHY
????????????????

I think this is a Globalize bug but I''m not 100% sure...

Someone can help me ????

Thanks in advance to everyone...

Tex



--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---