以下是我的User.rb
class User
has_one :profile
has_one :qualification
end
class Profile < ApplicationRecord
belongs_to :user
end
class Qualification < ApplicationRecord
belongs_to :user
end
现在,我想获取其profile.name如params或qualified.name的用户,如params。
我正在尝试以下代码,但收到错误:
User.joins(:profile, :qualification)
.where("profiles.name like ?
or qualifications.name like ?",
"%#{params[:name]}%",
"%#{params[:name]}%")
我正在使用Ruby 2.4, Rails 5.1
以下是错误:
SELECT `users`.* FROM `users` INNER JOIN `profiles` ON `profiles`.`user_id` = `users`.`id` INNER JOIN `qualifications` ON `qualifications`.`id` = `users`.`qualification_id` WHERE (profiles.name like %user9%) LIMIT 11
ActiveRecord::StatementInvalid: Mysql2::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 '%user9%) LIMIT 11' at line 1: SELECT `users`.* FROM `users` INNER JOIN `profiles` ON `profiles`.`user_id` = `users`.`id` INNER JOIN `qualifications` ON `qualifications`.`id` = `users`.`qualification_id` WHERE (profiles.name like %user9%) LIMIT 11
配置文件和限定表包含user_id作为外键。
我可以通过User.joins(:profile, :qualification)
创建一个