Rails 在加入模型中查询用户名字
Rails query user first name on join model
这种在连接模型上找到用户的情况似乎比平时更棘手
class ConversationParticipant < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
attr_accessible :user_id
end
class User < ActiveRecord::Base
has_many :conversation_participants
end
我正在尝试通过此查询 fisrt_name 查找 conversation_participant
user = User.where('conversation_participant like ? or first_name like ?', query, query)
但查询也没有 return user_participant 和用户 first_name。
有人可以给点提示!
您必须为 sql LIKE
运算符包含 %
query = params[:query] # or something ...
User.where('conversation_participant LIKE :query OR first_name LIKE :query', query: "%#{query}%")
这种在连接模型上找到用户的情况似乎比平时更棘手
class ConversationParticipant < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
attr_accessible :user_id
end
class User < ActiveRecord::Base
has_many :conversation_participants
end
我正在尝试通过此查询 fisrt_name 查找 conversation_participant
user = User.where('conversation_participant like ? or first_name like ?', query, query)
但查询也没有 return user_participant 和用户 first_name。
有人可以给点提示!
您必须为 sql LIKE
运算符包含 %
query = params[:query] # or something ...
User.where('conversation_participant LIKE :query OR first_name LIKE :query', query: "%#{query}%")