I'm using Ruby on Rails 4.2.3 and I have two string fields in users
table two_factor_secret_key
and two_factor_recovery_codes
. The first one has a string token while the second one has ten tokens separated by ;
. They look something like:
[62] pry(main)> ap User.last
User Load (6.0ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
#<User:0x007f8b3b009ad8> {
:id => 1,
:email => "test@me.com",
......
......
:two_factor_secret_key => "jvyzizqib47gp4au",
:two_factor_recovery_codes => "jgbcfjzh3lhpm6dv;er34rihizdlzu4fc;u32wwdmxl2dnxslv;liqcswrhjyfrpbkk;ttuezeszxnwzjent;4noxzmo4rg5jed5w;eynndg2gy5yzrdi3;h275valgjiiee7r3;f6m5xwujmtnucshe;tvhzxibvj4ikulyg"
}
How can I query users table in a way I search two_factor_secret_key
and two_factor_recovery_codes
.
In this example if the query was based on jvyzizqib47gp4au
I would like to return the user and the same if I use er34rihizdlzu4fc
I should get true as it's included in two_factor_recovery_codes
?
It's easy to find the record using two_factor_secret_key
with something like: User.where(two_factor_secret_key: "jvyzizqib47gp4au")
, however I'm not sure how should I query two_factor_recovery_codes
with ;
separated values.
Please note that I don't wanna use serialization with activerecords as ROR app is not the one who is responsible of generating the data in my case.
Any help?
您可以使用:
user = User.where("two_factor_secret_key LIKE :search OR two_factor_recovery_codes LIKE :search", search: "%jvyzizqib47gp4au%")
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.