简体   繁体   中英

How to get id from include? in rails

I having problem for getting element id from include? function in controller level.

def payslip_report
if params[:teacher_id] and params[:month_year].present?
   @month_and_year =   params[:month_year].gsub(' ','')
   @teacher_data   =   TeacherPayslip.where(:teacher_id => params[:teacher_id])
   @salary = Array.new
   @teacher_data.each do |i|
   a = i.salary_date.strftime("%B%Y")
   @salary << a 
end
   #raise @salary.inspect
   if @salary.include?(@month_and_year)
    raise "1"
  else
    raise "2"
  end

end

end

In if @salary.include?(@month_and_year) means, if it's true means, how to get id for included element.Using that id I can get all attribute for that id. Please guide me..

You can use select to filter out matching records:

@matched_records = @teacher_data.
                   select{|t| t.salary_date.strftime("%B%Y") == @month_and_year}
# => returns an array of TeacherPayslip records with matching month and year

if @matched_records.blank?
   //no match found
else
   first_match = @matched_records.first
end

Other, possibly better, solution is to do it all in db query by using month and year functions on the salary_date .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM