如何在rails控制器中选择?

时间:2012-04-21 20:50:23

标签: ruby-on-rails model-view-controller

现在我有这样的控制器方法:

     def modelv
    @model = Model.find(:all, :conditions => { :MOD_MFA_ID => params[:man]}) 
    @ct = CountryDesignation.find(:all, :conditions => { :CDS_ID => "110000002"})
    @destext = DesText.find(:all, :conditions => { :TEX_ID => "388555"})
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @model }
    end
  end

但我希望它看起来像这样:

 def modelv
    @model = Model.find(:all, :conditions => { :MOD_MFA_ID => params[:man]}) 
    @ct = CountryDesignation.find(:all, :conditions => { :CDS_ID => @model.MOD_CDS_ID})
    @destext = DesText.find(:all, :conditions => { :TEX_ID => @ct.CDS_TEX_ID})
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @model }
    end
  end

但我的模型结构是:

COUNTRY_DESIGNATIONS has_many MODELS

DES_TEXTS has_many COUNTRY_DESIGNATIONS

制造商有很多型号

因此,如果我选择@model - 它是数组,如果选择@ct - 它的数组(对于每个模型),如果选择@destext - 它是数组。如何正确选择这个。以及如何在视图中显示这个?现在我的观点看起来像这样:

%p#notice= notice

%h3
  - @model.each do |model| 
    %tr
      %p
        mod_id
        %td= model.MOD_ID
        name
        -#%td= model.country_designations.des_texts.TEX_TEXT
      = link_to 'Show model', model
= link_to 'Back', manufacturers_path

我看起来不会这样:

%p#notice= notice

%h3
  - @model.each do |model| 
    %tr
      %p
        mod_id
        %td= model.MOD_ID
        name
        %td= @destext.TEX_TEXT
      = link_to 'Show model', model

= link_to 'Back', manufacturers_path

1 个答案:

答案 0 :(得分:0)

如果MODELS属于COUNTRY_DESIGNATIONS且COUNTRY_DESIGNATIONS属于DES_TEXTS,那么find应该只返回一个结果。然后你不需要找到(:all,...),你需要

@model = Model.find(:first, :conditions => { :MOD_MFA_ID => params[:man]}) 
@ct = CountryDesignation.find(:first, :conditions => { :CDS_ID => "110000002"})
@destext = DesText.find(:first, :conditions => { :TEX_ID => "388555"})