0

我有医生表和用户表,以便用户属于医生和医生有一个用户作为个人资料。(多态关系)

我想要来自医生表和用户表的数据,其中医生.id = users.profile_id

@search_query =  params[:doctor][:query]
@users = User.select("profile_id, username").where("username like ? OR address like ?", @search_query, @search_query )
@doctor = Doctor.select("doctors.start_time, doctors.end_time, doctors.detail, doctors.experience, doctors.license_number, doctors.fee, doctors.available_days, doctors.specialization, doctors.mission_statement, doctors.id, users.id AS user_id, users.name, users.email, users.username, users.address, users.age, users.gender").joins(:user).where("? = doctors.id", @users.each do |u| u.profile_id end)#.joins(:user).where(:users => {:users.profile_id => @doctor.id})

医生移民

 create_table(:users) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""
  t.string :username, :null => false
  t.string :address
  t.integer :age
  t.string :gender
  t.string :name
  t.integer :profile_id
  t.string :profile_type

用户迁移

class CreateDoctors < ActiveRecord::Migration
  def change
    create_table :doctors do |t|
      t.time :start_time
      t.time :end_time
      t.text :detail
      t.integer :experience
      t.text :mission_statement
      t.string :license_number
      t.integer :fee
      t.string :available_days
      t.string :specialization

      t.timestamps
    end
  end
end
4

1 回答 1

1
Doctor.joins("LEFT OUTER JOIN users ON doctors.id = users.profile_id")
于 2013-11-06T07:39:33.450 回答