0

I want to find array, but gow can i do this?

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

but :CDS_ID => "110000002" is not good. I need to select it via @model.Field, for example: :CDS_ID => @model.Field. But also @model is not just one entry, it's an array. So I need for every Model select CountryDesignation. But then I need to select from CountryDesignation array DesText array

@destext = DesText.find(:all, :conditions => { :TEX_ID => @ct.Field})

How to do this work? And how to correct view this?

%table
  %tr
    %th Mfa id
    %th Год начала выпуска
    %th Год завершения выпуска
  - @model.each do |model| 
    %tr
      %td= link_to model.MOD_ID, model
      %td= link_to model.MOD_PCON_START, model
      -if model.MOD_PCON_END.blank?
        %td= link_to "По настоящее время", model
      -else
        %td= link_to model.MOD_PCON_END, model
      -#%td= model.country_designations.des_texts.TEX_TEXT
      -#= link_to 'Show model', model
  %br
  - @destext.each do |t|
    name
    %td= t.TEX_ID
    %td= t.TEX_TEXT
  - @ct.each do |ct|
    ct
    %td= ct.CDS_ID
    %td= ct.CDS_TEX_ID

2 Answers 2

2

You can use map. If the model attribute you're interested in were named cds_id:

@models = Model.all(:conditions => { :MOD_MFA_ID => params[:man] })
@ct = CountryDesignation.all(:conditions => { :CDS_ID => @models.map(&:cds_id) })

This essentially grabs the cds_id attribute of each model into an array and uses those values in a SQL IN clause. It returns an array of CountryDesignation objects that match.

Sign up to request clarification or add additional context in comments.

8 Comments

hm, no, I need field MOD_CDS_ID, so :CDS_ID => @models.MOD_CDS_ID like this
Okay, then just replace cds_id with MOD_CDS_ID in my example. I don't know what what your models look like, so I'm just giving an example.
like this all trouble is existing db(((
COUNTRY_DESIGNATIONS has_many MODELS DES_TEXTS has_many COUNTRY_DESIGNATIONS MANUFACTURERS has_many MODELS
how can i format this it in view, so that every model displays it ct and destext fields?
|
0

What you're looking for is a way for ActiveRecord to handle the array in the same manner as an IN clause in SQL. Luckily, AR does that by default when passed an array. In other words, you should be able to write:

@model = Model.find(:first, :conditions => { :MOD_MFA_ID => })

8 Comments

Oops, bad formatting. There was supposed to be an array before the closing }
Pavel, my solution is essentially the same as what you got from Brandan. In other words, to fit your problem, the answer will still be: CountryDesignation.all(:conditions => { :CDS_ID => @models.map(&:MOD_CSD_ID) })
and what to do if ct always have uniq if, but one of it's field reffers to des_text, and this field can be equal for many ct.id
how can i format this it in view, so that every model displays it ct and destext fields?
Are you asking how to get unique des_text rows based on the values in ct? Try: @destext = DesText.find(:all, :conditions => { :TEX_ID => @ct.map(&:Field).uniq}) ...This will result in an array of keys that are unique. Also, I would suggest reading up on ActiveRecord associations - it looks like there's a lot of overlap between what you're trying to do and associations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.