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
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.
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 => })
Related
I'm trying to I'm trying to show table, but I get an error
undefined method `name' for "Sphere Kharkov":String
This is my Instance Variables:
#result1 {"No department"=>"1", "Dep2"=>"1", "Sphere Kharkov"=>"2"}
#result2 {"Sphere Kharkov"=>"1"}
#department_names ["Sphere Kharkov", "Dep2", "Dep without members", "No department"]
Controller's methods
def age_profile
#result1 = age_report(1,9)
#result2 = age_report(10, 12)
#department_names = current_company.departments.map(&:name)
#department_names << "No department"
end
def age_report(start_age, end_age)
result = {}
User.select("COALESCE(departments.name, 'No department') AS name, COALESCE(count( * ), '0') AS count")
.joins("LEFT JOIN departments ON departments.id = users.department_id")
.where(:users => { :is_deleted => 0, :company_id => current_company.id})
.where("TIMESTAMPDIFF(YEAR, `date_of_birth`, CURDATE()) BETWEEN :start_age AND :end_age", start_age: start_age, end_age: end_age)
.group("departments.name")
.each {|d| result[d.name] = d.count }
result
end
In View:
%table.table.table-striped.table-bordered
%tr
%th= t('Depatment')
%th= t('1-18')
%th= t('18-25')
%th= t('25-45')
-#department_names.each do |f|
%tr
%td= f.name
%td= #result1[f.name]
%td= #result2[f.name]
Сould you help me please. What's wrong?
#department_names variable contain strings that don't respond to name method. Simple solution is:
- #department_names.each do |f|
%tr
%td= f
%td= #result1[f]
%td= #result2[f]
This line:
#department_names = current_company.departments.map(&:name)
Retrieves only the names of the companies. So basically you are calling the name method on the result of the name method.
So either remove .map(&:name) or just use f insteaf of f.name.
I have something like this:
%section
%h1= t('.MyTableData')
.row-fluid
.span8
%table.table
%thead
%tr
%th= sortable('teachers', 'teacher_name', 'true')
%th= sortable('teachers', 'teacher_score')
%th= sortable('teachers', 'specialty')
%tbody
- #teachers.each do |teacher|
%tr
%td= as_full_name(teacher[:first_name], teacher[:last_name])
%td= number_to_percentage(provider[:teacher_score], precision: 0)
%td= provider[:specialty_name]
.span2
=render partial: 'search'
So it creates a table with some columns, if I click on the column headers like teacher_name, etc it will sort the table based on that using that sortable method.
Now I want to add a simple twitter bootstrap button saying "Reset Sorting", and when I click that it should reset these sorting we have done by clicking on column headers and go and sort it only by teacher_name.
I am very new to all this and can't put these pieces of puzzle together, so I need a button, and the sort method I need there is some samples of it in that sortable methods, But can't figure out how to put all these together and solve this.
def sortable(table, column, default_column = false)
table_params = params[table] || {}
same_sort_column = same_sort_column(table_params, column, default_column)
current_sort_direction = sort_direction(table_params)
new_direction = same_sort_column && current_sort_direction == 'asc' ? 'desc' : 'asc'
link_to(t(".#{column}"), params.merge(table => { sort_column: column, sort_direction: new_direction }))
end
= link_to 'Reset sorting', params.merge(:teachers => {:sort_column => 'teacher_name', :sort_direction => 'asc'}), :class => 'btn'
I now, that question could be non good for so, but need help:
In my rails haml view i have such code:
%table.table.table-striped
= form_tag :admin_other_price_upload do
%tr
- #csv.first.length.times do |n|
%th
= n + 1
%br/
=select_tag "s"+(n+1).to_s, options_for_select([["Брэнд", "Brand"], ["Артикул","Article"], ["Наименование","Descr"], ["Цена","Price"], ["Количество","Quantity"], ["Дополнительно","Other"], ["Поле кроссов","Cross"]]), :prompt => 'Все', :id => "select-value"
*********************************
so as you can see i'm setting to all select's name like s(n+1) and value one from list. But how can i get them both in controller method? I need it becouse i have dynamic table. I will explain it on example:
So i have table with select's
name = s1 (value = Brand) | name = s2 (value = Price)
so i need in controller to get not only that s1 => Brand, but also get this 1 from s1
So if param look's like
[
s1 => {Brand}
]
I need to get for my calculation s1 not value, but s1 as string (simple i need to find in params, which value has Brand and select it as a value)
So for Brand i need to select s1, and set as s1 value s1, how could i do it?
I may have understood you, but not sure.
# let's say your params hash is like:
params = { :action => 'show', :controller => 'articles',
:s1 => 'Article', :s2 => 'Brand', :s3 => 'Price', ... }
brand_param = params.select{ |key, value| value == 'Brand' }
# => { :s2 => 'Brand' }
which_s_is_brand = brand_param.keys.first.to_s
# => 's2'
I have selected for every model it's data from another table using this in controller:
def modelv
#model = Model.find(:all, :conditions => { :MOD_MFA_ID => params[:man]})
#ct1 = CountryDesignation.all(:conditions => { :CDS_ID => #model.map(&:MOD_CDS_ID)})
#ct = #ct1.uniq{|hh| hh.CDS_ID}
#destext = DesText.find(:all, :conditions => { :TEX_ID => #ct.map(&:CDS_TEX_ID)})
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #model }
end
end
sorry for dirty code.
and my view
%table %tr
%th Mfa id
%th Год начала выпуска
%th Год завершения выпуска
- #model.each do |model|
%tr
%td= model.MOD_CDS_ID
%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 %table %tr
- #destext.each do |t|
%tr
%td= t.TEX_ID
%td= t.TEX_TEXT
- #ct.each do |ct|
%tr
%td= ct.CDS_ID
%td= ct.CDS_TEX_ID
But how to display for every model its t.TEX_TEXT and ct.CDS_TEX_ID, not in another loop, but in same table, in column? And moreover, my method is fetching data, but when I select data, when ct.CDS_TEX_ID is same for some more than one entry, #destext is grouping, and i fetch only one instance, for example
cds_id cds_tex_id
110007626 420077
110007627 420077
and as result i get from #destext only one row. Help me please. It's non-standart db
See, table MODELS has field MOD_CDS_ID, with this field I go to table COUNTRY_DESIGNATIONS and it's field CDS_ID = MOD_CDS_ID (from MODELS table), than from this table I select field CDS_TEX_ID and go with this id to table DES_TEXTS it's field TEX_ID must be equal from previous table CDS_TEX_ID, and fetch field TEX_TEXT !!! And this field in my iteration #model.each do.... i must display for every model
I know how to access foreign key attributes in a scaffold index view. I can simply refer to the attributes using dot notation such as property.que.name. Given the following models:
class Priority < ActiveRecord::Base
belongs_to :que
...
end
class Que < ActiveRecord::Base
has_many :priorities
...
end
In the index view, I can do something like this to get the name value:
<td><%=h priority.que ? priority.que.name : "" %></td>
How do I do this in the jqgrid?
I tried this but the jqgrid comes back empty:
Priorities Controller:
#priorities = Priority.find(:all, :order => "position", :conditions => "multitenant_team_id = " + current_user.team.id.to_s ) do
if params[:_search] == "true"
id =~ "%#{params[:id]}%" if params[:id].present?
issue_id =~ "%#{params[:issue_id]}%" if params[:issue_id].present?
que =~ "%#{params[:que]}%" if params[:que].present?
customer =~ "%#{params[:customer]}%" if params[:customer].present?
title =~ "%#{params[:title]}%" if params[:title].present?
reporting_source =~ "%#{params[:reporting_source]}%" if params[:reporting_source].present?
priority =~ "%#{params[:priority]}%" if params[:priority].present?
product =~ "%#{params[:product]}%" if params[:product].present?
current_owner =~ "%#{params[:current_owner]}%" if params[:current_owner].present?
end
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
if request.xhr?
end
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #priorities.to_jqgrid_json(
[:id, :issue_id, :que.name, :customer, :title, :reporting_source,
:priority, :product, :current_owner],
params[:page], params[:rows], #priorities.total_entries)}
format.xml { render :xml => #priorities }
end
end
Index View:
<%= jqgrid("Priorities", "priorities", "/priorities",
[
{:field => "id", :label => "ID", :width => 35, :resizable => false},
{:field => "issue_id", :label => "Issue Id"},
{:field => "que", :label => "Queue"},
{:field => "customer", :label => "Customer"},
{:field => "title", :label => "Title"},
{:field => "reporting_source", :label => "Reporting Source"},
{:field => "priority", :label => "Priority"},
{:field => "product", :label => "Product"},
{:field => "current_owner", :label => "Current Owner"}
],
{ :rows_per_page => 12, :height => 450 }
)%>
If I specify que instead of que.name, I get the data back in the grid but the Queue field shows a "#" symbol so I suspect the .to_jqgrid_json call doesn't like my syntax.
Has anyone tried this before? I hope so.
I fixed my problem. I ended up changing my find to a find_by_sql so I could do a left outer join on the ques table. I think there were a couple of issues. I think the *to_jqgrid_json* had problems with null foreign key values and I couldn't figure out how to get at the Que.name any other way. I'm using SQLServer so I had to use isnull(ques.name, '') to convert the null to empty space.
So I replaced my find as follows:
#priorities = Priority.find_by_sql ["select priorities.id id, issue_id, isnull(ques.name,' ') queue_name, customer, title, reporting_source, priority, product, current_owner from priorities left outer join ques on priorities.que_id = ques.id where priorities.multitenant_team_id = ? order by issue_id", current_user.team.id.to_s]
This introduced another problem in that find_by_sql returns an array which breaks the #priorities.total_entries call. So I had to replace it with array.count.
format.json { render :json => #priorities.to_jqgrid_json(
[:id, :issue_id, :queue_name, :customer, :title, :reporting_source, :priority, :product, :current_owner],
params[:page], params[:rows], #priorities.count)}
My grid looks great!
Edit
My grid LOOKS great but it doesn't paginate or sort. Back to the drawing board. :(
Okay, I think I fixed it for real this time.
#priorities = Priority.find(:all,
:select => "priorities.id, priorities.issue_id,
priorities.customer, priorities.title,
priorities.reporting_source, priorities.priority,
priorities.product, priorities.current_owner,
priorities.position,
isnull(ques.name,' ') queue_name",
:joins => "LEFT OUTER JOIN ques ON ques.id = priorities.que_id",
:order => "priorities.position",
:conditions => "priorities.multitenant_team_id = " + current_user.team.id.to_s ) do
I had know idea I could specify joins like this. This keeps the resultset in a format the 2dc_jqgrid plugin likes. Sorting, pagination and searching all work now. Now my grid looks good and actually works.