0

I have project and city table, and many-to-many relation between.

I want in html to get list of cities names, divided with ", ".

I tried with this:

<%= @project.cities(&:name).join(", ") %>

But I get (I think) object like this:

#<City:0x103886748>

Where I made a mistake? :|

P.S. Explanation:

I have @project that have one or more cities. I want to loop through cities and print names like this: New York, Boston, Belgrade (without comma on the end).

0

2 Answers 2

3

You've forgot map here

<%= @project.cities.map(&:name).join(", ") %>
Sign up to request clarification or add additional context in comments.

Comments

0

There is also a very cool built in helper to do that.

to_sentence

http://api.rubyonrails.org/classes/Array.html#method-i-to_sentence

<%= @project.cities(&:name).map.to_sentence %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.