私は2つのモデルを持っています:
class User < ActiveRecord::Base
has_one :user_profile
end
class UserProfile < ActiveRecord::Base
belongs_to :user
end
aUser
は oneUserProfile
を持ち、 a は aUserProfile
に属しますUser
テーブル構造:
ユーザー
id : 整数 name : 文字列 email : 文字列
ユーザープロフィール
id : 整数 user_id : 整数 bio : 文字列 blog_url : 文字列
Rails コンソールを使用して、両方のテーブルを結合してユーザーの情報を取得しようとしました。
User.joins(:user_profile).includes(:user_profile).where(user_profiles: {user_id: 1})
コンソール画面の結果には、User テーブルのみが表示され、UserProfile テーブルは表示されません。
=> [#<User id: 1, name: "Alan", email:"alan@example.com">]
コンソール画面に表示された SQL ステートメント (SELECT "users"."id" bla bla...) は正しいようです。これをコピーして SQLite ブラウザーに貼り付けてコマンドを実行すると、次のように結果が表示されます。期待される。
|id| name | email | id | user_id | bio | blog_url |
------------------------------------------------------------------------------
|1 | Alan | alan@example.com | 1 | 1 | lorem ipsum | alan.example.com |
そのため、ここで2つの異なる結果が見つかりました。
これらのテーブルがコンソールに参加しない原因は何ですか?