sql - ActiveRecord Joins -
ok, so, if user.joins(:session_users)
, attributes of users table.
how attributes of both tables in activerecord way, i.e., not sql?
edit one
ok, based on first answer, i'm trying have displayed.
so, method written in users controller
def blah @users = user.includes(:session_users) @users.each |user| user.session_users end end
then have in users view blah.html.erb
<%= @users.session_users %>
and in routing section:
match "/users/blah" => "users#blah"
i think want includes
instead of joins
. see http://railscasts.com/episodes/181-include-vs-joins more info. should fetch columns both,
users = user.includes(:session_users) users.each |user| user.session_users end
note, still performs 2 sql queries.
edit
updated answer assumes user
has_many :session_users
routes:
# config/routes.rb '/users/blah' => 'users#blah'
controller:
# app/controllers/users_controller.rb class userscontroller < applicationcontroller def blah @users = user.includes(:session_users) end end
view:
# app/views/users/blah.html.erb <% @users.each |user| %> <%= user.name %> // assumes user has name attribute <% user.session_users.each |session_user| %> <%= session_user.attributes %> // prints attributes <%= session_user.created_at %> // assumes user has created_at attribute <% end %> <% end %>
Comments
Post a Comment