开发者

Show Name instead of ID in a HABTM relationship

开发者 https://www.devze.com 2023-02-03 18:52 出处:网络
Excuse me if I\'m being too much of a beginner but none of the other related answers worker. I want to show the category name that the links belong to instead of the id.

Excuse me if I'm being too much of a beginner but none of the other related answers worker.

I want to show the category name that the links belong to instead of the id.

Here's the migration.

class CreateCategoriesLinks < ActiveRecord::Migration
  def self.up
    create_table :categories_links, :id => false do |t|
  t.references :category
  t.references :link
  end
 end

def self.down
  drop_table :categories_links
end

end

The categories model

class Category < ActiveRecord::Base
  has_and_belongs_to_many :links
end

The links model

class Link < ActiveRecord::Base     
 has_and_belongs_to_many :categories
end

And here's what's in the links controller under index and show

@categories = Category.find(:all, :order => 'name')

and here's what's in the index right now but, I've tried every permutation of this that I could find.

<%= li开发者_开发百科nk.category.name %>

If it put <%= link.category_ids %>, it'll show the ids.


Try:

<% link.categories.each do |cat| %>
  <%= cat.name %><br>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消