简体   繁体   中英

How do i make a foreign/primary key case insensitive in rails

I'm using the following code:

in one model

class Foo < AR::Base
    has_many :bars, foreign_key: :email, primary_key: :email, dependent: :destroy
end

And in another model:

class Bar
    belongs_to :foo, foreign_key: :email, primary_key: :email
end

How do i make it so that the email is case insensitive as the foreign key and primary key for both models?

I use utf8_unicode_ci collation for my tables (ci stands for case insensitive) which allows me to query without being concerned about the case of the text. If you make your table or at least column case insensitive, then it should just work with queries...

(Note this works for MySQL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM