1

저는 지금 당분간이 문제를 해결해 왔으며이를 파악하지 못했습니다. 아마도 단순한 것이지만 여기에 있습니다 :has_many의 조회 : 관계를 통해

저는 '라미네이트'와 '표준'사이에 has_many : trough 관계가 있습니다.이 표준 모델은 '표준화'모델입니다.

Standard.rb

class Standard < ActiveRecord::Base 
attr_accessible :description, :name 
has_many :standardizations 
has_many :laminates, :through => :standardizations 
end 

Standardization.rb

class Standardization < ActiveRecord::Base 
    attr_accessible :laminate_id, :standard_id 
    belongs_to :laminate 
    belongs_to :standard 
end 

Laminate.rb

class Laminate < ActiveRecord::Base 
attr_accessible :name, :standard_ids 
has_many :standardizations 
has_many :standards, :through => :standardizations 
end 

scen ario는 라미네이트가 여러 표준에 속할 수 있다는 것을 보여 주며 새로운 부분에서 작동하는 모든 것을 확인할 수 있습니다. 내 문제는 주어진 라미네이트에 대한 해당 표준의 이름을 표시하려고 할 때입니다. 현재로서는 라미네이트가 어느 표준에 할당 되었는가를 표시 할 수 있지만 표준의 이름 만 표시 할 수는 없습니다.

<%= @laminate.standards %> 

을 그리고 이것은 올바른 모든하지만

<%= @laminate.standards.name %> 

이 ... 작동하지 않는 말을 반환

내 show.html.erb는 말한다. 어떻게 지구상에서 각 개인의 이름을 알 수 있습니까?

Laminate_controller :

class LaminatesController < ApplicationController 
# GET /laminates 
# GET /laminates.json 
def index 
@laminates = Laminate.all 
@standards = Standard.all 

respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @laminates } 
end 
end 

# GET /laminates/1 
# GET /laminates/1.json 
def show 
@laminate = Laminate.find(params[:id]) 
@standard = Standard.find(params[:id]) 

respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @laminate } 
end 
end 

# GET /laminates/new 
# GET /laminates/new.json 
def new 
@laminate = Laminate.new 


respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @laminate } 
end 
end 

# GET /laminates/1/edit 
def edit 
@laminate = Laminate.find(params[:id]) 
end 

# POST /laminates 
# POST /laminates.json 
def create 
@laminate = Laminate.new(params[:laminate]) 

respond_to do |format| 
    if @laminate.save 
    format.html { redirect_to @laminate, notice: 'Laminate was successfully created.' } 
    format.json { render json: @laminate, status: :created, location: @laminate } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @laminate.errors, status: :unprocessable_entity } 
    end 
    end 
end 

# PUT /laminates/1 
# PUT /laminates/1.json 
def update 
@laminate = Laminate.find(params[:id]) 

respond_to do |format| 
    if @laminate.update_attributes(params[:laminate]) 
    format.html { redirect_to @laminate, notice: 'Laminate was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @laminate.errors, status: :unprocessable_entity } 
    end 
end 
end 

# DELETE /laminates/1 
# DELETE /laminates/1.json 
def destroy 
@laminate = Laminate.find(params[:id]) 
@laminate.destroy 

respond_to do |format| 
    format.html { redirect_to laminates_url } 
    format.json { head :no_content } 
    end 
end 
end 

답변

5

[표준 목록을 반환 @laminate.standards 다음과 같습니다. 이 목록에 각 루프를해야 작동하지 않을 수 있습니다 여기에 이름을 전화 :

# replace the following: 
<%= @laminate.standards.name %> 
# with this code: 
<% @laminate.standards.each do |standard| %> 
    <%= standard.name %> 
<% end %> 

당신이 짧은 버전 미만 사용자 정의하려면 다음

<%= @laminate.standards.map{ |standard| standard.name }.join(', ') %> 
# This will show all the standards' name with a coma-space ',' between it 

# same a above but shorter: 
<%= @laminate.standards.map(&:name).join(', ') %> 
# this will call the 'name' method on each standard of @laminate.standards 
# and join them with a coma-space 
# something that would look like this: 
# name1, name2, name3 
+1

예를, 그래, 그래! 고맙습니다! – trymv

+0

네, 네, 네 여기 있습니다! 고맙습니다! – masaaki

관련 문제