I have two models:
class User < ActiveRecord::Base
has_one :user_profile
end
class UserProfile < ActiveRecord::Base
belongs_to :user
end
a User
has one UserProfile
, and a UserProfile
belongs to a User
The tables structure:
User
id : integer name : string email : string
UserProfile
id : integer user_id : integer bio : string blog_url : string
Using rails console, I try to get the information of a User by joining both tables:
User.joins(:user_profile).includes(:user_profile).where(user_profiles: {user_id: 1})
The result on the console screen shows me only the User table, the UserProfile table doesn't show up.
=> [#<User id: 1, name: "Alan", email:"alan@example.com">]
The SQL statement that appeared on the console screen (the SELECT "users"."id" bla bla...) seems right, since when I copy and paste it to SQLite browser and execute the command, it shows me the result as I expected.
|id| name | email | id | user_id | bio | blog_url |
------------------------------------------------------------------------------
|1 | Alan | alan@example.com | 1 | 1 | lorem ipsum | alan.example.com |
So I found two different results here.
What might cause these tables not joining on my console?