Chat.find_by(id: 6).chatusers returns collection of table “chats_users”
and
this is fine working.
But i can not get model user by each element of collection in method
“each”.
So, question is why each not working to get relative model “user” or how
to
resolve my task by each or another way.
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.
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)
On 4 September 2015 at 15:32, Николай Спелый [email protected]
wrote:
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.
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.