There are no errors given. Instead rails puts result of Chat.find_by(id:
6).chatusers, not result of "each", that's strange.
Now I look again that is not surprising. The method inspect outputs
to the server terminal window not to the html for display, if you look
in the server window you should see it. You need to do something like
<% Chat.find_by(id: 6).chatusers.each do |chat_user| %>
<%= chat_user.user.name %>
<% end %>
If you are trying to get debug out then the best way is
<% logger.info Chat.find_by(id: 6).chatusers.each { |chat_user|
chat_user.user.inspect %>
which will appear in development.log. You can also use logger.info in
model or controller.
Great !
I already try write this logic but it not works. But when i copy-paste your variant it’s work.
So difference is in formating code, i write in one line, you write in three lines.
That’s nice for resolving problem, but why it’s works in divided style of ruby and not works in one-line style ?
My one-line is <%= Chat.find_by(id: 6).chatusers.each { |chat_user| chat_user.user } %> which about i told.
So you have:
<%= something %>
which ERB interprets as call .to_s on the result of evaluating something and add that to the output (possibly after making it HTML-safe depending on your Ruby on Rail version)
The problem is that ruby is swallowing whatever happens inside the .each --
what you want is probably .map instead. The return value from .each is the
collection. The return value from map is a new collection of the results of
the block.