Rails在连接模型上查询用户名

时间:2017-01-19 22:10:04

标签: mysql ruby-on-rails ruby postgresql ruby-on-rails-3

在连接模型上找到用户的这种情况似乎比平时更棘手

static fromJsonList(array): Hero[] {
  return array.map(Hero.fromJson);
}

//add more parameters depending on your database entries and Hero constructor
static fromJson({id, name}): Hero {
  return new Hero(id, name);
}

我试图通过此查询找到带有fisrt_name的conversation_participant

 class ConversationParticipant < ActiveRecord::Base
      belongs_to :user
      belongs_to :conversation
      attr_accessible :user_id
    end



   class User < ActiveRecord::Base
      has_many :conversation_participants

  end

但是查询没有返回user_participant和用户first_name。

有人可以请一点提示!

1 个答案:

答案 0 :(得分:0)

您必须为sql %运算符

添加LIKE
query = params[:query] # or something ...
User.where('conversation_participant LIKE :query OR first_name LIKE :query', query: "%#{query}%")