I'm going to be a heretic here and propose that your database is already doing 
exactly what you need. Just catch the error from MySQL and be happy.

On Nov 18, 2009, at 23:44 , Rob Kaufman wrote:

>   The problem is that Rails validations use Ruby constructs to test the 
> values... they don't use or really know that much about the MySQL settings.  
> There are two ways around this issue.  The first way to use a custom 
> validation, that is probably the right thing to do.  The second would be to 
> override the accessor to strip out any accent codes on assignment.  Basically 
> you'd setup your own custom name= method.
> 
> Good Luck,
> Rob
> 
> On Tue, Nov 3, 2009 at 20:42, jaambros <[email protected]> wrote:
> 
> Hi all,
> In the aacg_courses model I have:
>  validates_uniqueness_of :name, :case_sensitive=>:false
> in the migration I have an index with :unique options
>  add_index :aacg_courses,
> [:name, :language_id ],:name=>"aacg_courses_idx" , :unique => true
> 
> when trying to save a record with a name that differs to one in the
> DB  by only an accent (or ñ) I get an error:
> Mysql::Error: Duplicate entry 'Geografia-8-core-1' for key
> 'aacg_courses_idx': INSERT INTO aacg_courses (name,
> acad_area_cycle_grade_id, type_course, language_id) VALUES
> ('Geografia', 8, 'core', 1)
> 
> The DB had a record with name Geografía (note the accent on the i) and
> I was trying to save one without the accent; call them G1 and G2
> It seems that validate_uniqueness_of finds G1 and G2 are diff but
> MySQL finds they are the same and issues a uniquess violation.
> MySQL's behavior is correct in my view as I'm using UTF8/collation
> (which ignores accents) throughout in my app.
> I have UTF8 in all the right places in Rails I think:
> $KCODE = "UTF-8" in environment.rb,  and
> def configure_charsets
>   headers["Content-Type"] = "text/html; charset=utf-8"
>   ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
> end
> in app/controllers/application_controller
> 
> It seems that rails uniqueness is not mached to that of the DB in this
> case.
> Is there an elegant way of getting this right? [short of writing a
> custom validation]

Regards,

Guyren G Howe
Relevant Logic LLC

guyren-at-relevantlogic.com ~ http://relevantlogic.com ~ +1 512 784 3178

Ruby/Rails,  REALbasic, PHP programming
PostgreSQL, MySQL database design and consulting
Technical writing and training

Read my book, Real OOP with REALbasic: 
<http://relevantlogic.com/oop-book/about-the-oop-book.php>

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to