new ruby on rails user here. I have a question about the following few lines of code.
First example in view.
<% sailing.travelling_parties.each do |party| %>
<% party.party_registers.each do |reg| %>
<%= reg.user.detailed_user.first_name %>
<% end %>
<% end %>
When I run it in the in view, i get my expected result which is multiple names printed out. When I move it to the model and change it to the following,
Second example, in model. The ** represents what I'm actually trying to accomplish in this entire example.
class Sailing < ActiveRecord::Base
...
def gather_data
self.travelling_parties.each do |party|
party.party_registers.each do |reg|
reg.user.detailed_user.first_name
** use a variable to count loop iterations **
end
end
end
Second example, in View
<%= sailing.gather_data %>
This is printing out an array filled with the 'travelling_parties' I am trying to iterate over, not what I had expected at all! So, obviously it only loops once regardless of how many names are in the sailing record.
Can any shed any light on why this is happening, and how I can get it to work like the first example, without actually putting my business logic in the view?
Thanks