我有医生表和用户表,这样用户所属的医生和医生has_one用户作为个人资料。 (多态关系)
我想要来自医生表的数据以及用户表,其中doctor.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
答案 0 :(得分:1)
Doctor.joins("LEFT OUTER JOIN users ON doctors.id = users.profile_id")