I want to join records together and separate them with “-“
I know how to join one table records together like this:
@keywords = @tweet.hash_tags.join("-")
But what if it’s HABTM associated tables.
For example.
// BRAND MODEL
has_and_belongs_to_many :categories
// CATEGORY MODEL
has_and_belongs_to_many :brands
If I do this:
@brands = Brand.all
@brand_categories = @brands.categories.join("-")
I get this result:
#<Category:0x0000010445c928>,#<Category:0x0000010445c7c0>,#<Category:0x0000010445c5e0>,#<Category:0x0000010445c400>,#<Category:0x0000010445c270>
Hope you understand my question – thanks.
#joinwill call#to_son the items in theArrayreturned by@brands.categoriesby default, and it doesn’t look like you’ve defined a customCategory#to_s. Either do so, or be more explicit about the string representation you want; if, for example, aCategoryhas atitleattribute, you could use: