I have an issue with the includes
method in Rails.
I have this user model:
class User < ActiveRecord::Base
has_one :account
end
And this account model:
class Account < ActiveRecord::Base
belongs_to :User
end
Now the user model has :id
and :names
and the account model has :user_id
and :country
In another controller i want to fetch and display all the user names with their countries.
I am using this query
@users = User.includes(:account).where(some condition)
Please tell me what can I add in the query to fetch all the countries associated with the user names? Thanks in advance
user should be plural in the controller as you are getting list of records . like this
@users = User.includes(:account)
in view you can do this.
<% @users.each do |user| %>
<%= user.names %>
<%= user.account.country %>
<% end %>
As names
is user attribute, you can fetch directly, while country is account's
attribute, you can fetch using user.account.country