I have two Models, Habit and HabitLog.
For simplicity Habit holds a name and has many HabitLogs. HabitLog holds a date and status.
I want to create a view for specific date and list all the Habits with status for given date.
To get records, but keep association I used this query in my controller:
def show
@habit_logs = HabitLog.where(log_date: '2015-07-15').includes(:habit).order("habits.position")
end
Then in my view I have
<ul>
<%= @habit_logs.each do |habit_log| %>
<li>
<%= habit_log.habit.name + " " + habit_log.status %>
</li>
<% end %>
</ul>
It works, meaning in lists all the Habit names and status for given day, but it pastes the whole array in a strange place, after the li tag, but inside ul.
<ul>
<li>
Waga 87
</li>
<li>
Higiena 1
</li>
<li>
Siłownia 0
</li>
[#<HabitLog id: 1, log_date: "2015-07-15", status: "87", habit_id: 1, created_at: "2015-07-15 11:37:21", updated_at: "2015-07-15 11:37:21">, #<HabitLog id: 2, log_date: "2015-07-15", status: "1", habit_id: 2, created_at: "2015-07-15 11:37:28", updated_at: "2015-07-15 11:37:28">, #<HabitLog id: 3, log_date: "2015-07-15", status: "0", habit_id: 3, created_at: "2015-07-15 11:37:30", updated_at: "2015-07-15 11:37:30">]
</ul>
I figure this has to do with my use of .includes(:habit), but I have no idea how to deal with it.
The problem is this line
<%= @habit_logs.each do |habit_log| %>
should be
<% @habit_logs.each do |habit_log| %>
Imp Note:
<% %> # Executes the code.
<%= %> # Prints the output.