Ruby on Rails Friday, September 4, 2015

On 2015-Sep-4, at 15:56 , Николай Спелый <peavey5150kh@gmail.com> wrote:

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)

Therefore, what is the value of 

Chat.find_by(id: 6).chatusers.each { |chat_user| chat_user.user }

Well, that's just something.each {|…| … } which has the value of something. In your case, the same as if you just had:

Chat.find_by(id: 6).chatusers

This is probably some ActiveRecord proxy for the association. The default .to_s is very likely similar to .inspect and looks something like:

#<ActiveRecord::… >

So plop that into your HTML and a browser sees the <…> as an unknown element and does nothing so you're left with:

#

in your page. Does that make sense to you?

-Rob

No comments:

Post a Comment