【问题标题】:join two tables to get data rails 4连接两个表以获取数据轨 4
【发布时间】:2013-11-11 01:39:14
【问题描述】:
class Guardian < ActiveRecord::Base
  has_many :patients
  has_one :user, as: :profile
  accepts_nested_attributes_for :user
end


class User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

用户迁移

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    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

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0, :null => false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
  end
end

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    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

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0, :null => false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip
      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    end
end

监护人迁移

class CreateGuardians < ActiveRecord::Migration
  def change
    create_table :guardians do |t|
      t.string :family_name

      t.timestamps
    end
  end
end

我想在单个变量中从用户表和监护人表中获取数据 监护人有一个用户,用户属于监护人作为个人资料(多态)。我想从用户表和监护人表中获取数据,其中 Guardian_id=users.profile_id

【问题讨论】:

  • 这是我的第一个问题,抱歉缩进。
  • 避免添加注释代码,它只会增加噪音。

标签: ruby-on-rails activerecord devise ruby-on-rails-4


【解决方案1】:

由于某种原因,上面的答案对我不起作用,所以我尝试了直接 sql 查询

sqlQuery = "select tableA.column1, tableB.column2 from tableA inner join tableB on tableA.some_id = tableB.id"
ActiveRecord::Base.connection.execute(sqlQuery)

【讨论】:

    【解决方案2】:

    试试

    Guardian.select("*").joins(:user)
    

    编辑:

    如果你有来自连接的同名列,你可以这样做

    Guardian.select("guardians.family_name, guardians.id as g_id, users.id as u_id,
        users.name, users.email, users.username, users.address, users.age,
        users.gender").joins(:user).where(:users => {:u_id => @user_session.id}) 
    

    【讨论】:

    • 是的,有效。但是如果我想要一些用户和监护人的属性。(只有监护人的family_name和id,用户的姓名)?
    • Guardian.select("guardians.*, users.id as user_id").joins(:user).having("user_id = (select id from users where id=guardians.id order by id desc limit 1)") 这将返回空对象。什么是错误:(
    • Guardian.select("guardians.family_name, users.id, users.name").joins(:user)
    • 但现在我有两个 id。用户 ID 和监护人 ID。如何命名一个ID。这是我的查询 Guardian.select("guardians.family_name, Guardians.id, users.id, users.name, users.email, users.username, users.address, users.age, users.gender").joins(: user).where(:users => {:id => @user_session.id})
    • @HithamS.AlQadheeb 如何在最后一个 where 子句中指定 ORwhere (:users =&gt; [{:u_id =&gt; @user_session.id}, {:name =&gt; "xyz"}]) 之类的东西?
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多