2012-01-31 3 views
0

나는 대답을했을 수도있는 (희망을 갖고) 간단한 질문이 있습니다. 나는 그것을 찾을 수 없었다 ... 글쎄, 여기에 간다.레일 - ID와 관련된 이름을 표시하는 방법

나는이 스키마

create_table "items", :force => true do |t| 
t.text  "description" 
t.string "priority" 
t.date  "date" 
t.time  "time" 
t.datetime "created_at" 
t.datetime "updated_at" 
t.boolean "done" 
t.integer "user_id" 
end 

create_table "users", :force => true do |t| 
t.string "name" 
t.datetime "created_at" 
t.datetime "updated_at" 
end 

이 내가 새로운 항목을 추가하는 내 양식이 있습니다, 그것을 작동, 데이터가 올바르게 저장되어 이제

<%= collection_select(:item, :user_id, User.all, :id, :name) %> 

을 (난 이미 설정 한 적절한 상관 관계). 하지만 원하는 것은 ID 번호 대신 항목이 할당 된 사람의 이름 인 항목 색인에 표시하는 것입니다.

<td><%= item.user_id. %></td> 

을하지만 난 오히려 뭔가에만

item.user.name 

처럼, 그것은 작동하지 않습니다 싶어 - 나는 내 컨트롤러에서 어떤 행동을해야 할 것 같아요 : 항목/인덱스에서

나는 있습니다. 제발 도와 주실 수 있습니까? :) 여기

편집 좀 더 자세한 내용입니다 : 내 모델 :

class User < ActiveRecord::Base 
has_many :items 
end 

class Item < ActiveRecord::Base 
belongs_to :user 
end 

답변

1

이 항목 클래스에이 추가 :

class Items < ActiveRecord::Base 
    belongs_to :user 
end 

그런 다음 item.user.name 작동합니다. item.rb 모델에서 사용자 및 has_many :

+0

이미 belongs_to이 항목 을 user.rb 모델 에서 여전히 작동하지 않습니다 :( – alemur

+0

을 내가 클래스 항목 <액티브 :: 자료를 \t belongs_to : 사용자 끝 – alemur

+0

이 /home/alex/Documents/Web/myapp/app/views/items/index.html.erb 곳을 표시 단수라는 이름의 클래스 '항목' 당신이 어떤 오류를받을 수 있나요 –

관련 문제