2011-01-13 3 views
3

너무 초보자이지만 다른 관련 답변자가 아닌 경우 실례합니다.HABTM 관계의 ID 대신 이름 표시

ID 대신 링크가 속한 범주 이름을 표시하려고합니다.

다음은 마이그레이션입니다.

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 

범주 모델

class Category < ActiveRecord::Base 
    has_and_belongs_to_many :links 
end 

class Link < ActiveRecord::Base  
has_and_belongs_to_many :categories 
end 

그리고 여기에 인덱스 아래 링크 컨트롤러이고

,219을 보여 무엇 링크 모델

여기에 색인에있는 내용이 있지만 지금까지 찾을 수있는 모든 순열을 시도했습니다.

<%= link.category.name %> 

<%= link.category_ids %>을 입력하면 ID가 표시됩니다.

답변

1

시도 :

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

감사합니다! 그것은 효과가있다! – Thomas