0

house model

class House < ActiveRecord::Base
   has_many :categories, :through => :category_join_table

def features_to_html_class  
  "#{price} #{rooms} #{guests} #{{categories.name do |cat| cat.name}}"
end
end

House index template

- @houses.each do |house|
  .item{:class => house.features_to_html_class }

The values price, rooms, guests are parsed correctly in the view (filtering purposes), but not the categories. What i am doing wrong ?

1

3 Answers 3

2

Try this:

categories.map{|cat|cat.name|}.join(' ')
Sign up to request clarification or add additional context in comments.

Comments

2

You don't want to have multiple classes for the same category name if there is a space.

Top Articles
Articles

Would produce:

class="Top Articles Articles"

You could go a step further and replace spaces with dashes, and downcase it all:

"#{ categories.map{ |cat| cat.name.parameterize }"

class="top-articles articles"

EDIT:

As Anthony pointed out in the comments, if you're using Rails, you can use parameterize.

1 Comment

or use String#parameterize to convert a string to a standardized representation
0

You should iterate through each name categori so you need to use:

{categories.name.each { |cat| cat.name}}

1 Comment

categories.name will surely raise a NoMethod error. And the each method will return the original array object. You should use map instead. Try it yourself on an irb console.

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.