models:
#StatusMessage model
class StatusMessage < ActiveRecord::Base
belongs_to :users
default_scope :order => "created_at DESC"
end
#User Model
class User < ActiveRecord::Base
has_many :status_messages
end
In controller I want to join these two tables and get fields from both table. for example I want email field from User and status field from StatusMessage. When I use :
@status = User.joins(:status_messages)
Or
@status = User.includes(:status_messages)
It gives me only the user table data.
How can I implement this requirement?
You need to use
includeshere. It preloads data so you won’t have another SQL query when you do@user.status_messages.And yes you can’t really see the effect: you need to check your logs.