簡體   English   中英

Rails范圍方法返回WhereChain而不是數組

[英]Rails scope method returns WhereChain instead of array

在我的Deal類中,我添加了一個范圍方法:

scope :current, -> { where {|d| d.end_date > Date.today} }

它返回正確的數據,但不是返回一個我可以計算,迭代等的數組,它返回一個ActiveRecord::QueryMethods::WhereChain ,我不能做任何事情。

我注意到對Deal.current的調用以“ @scope= ”開頭:

Deal.current                                                                                                                   
  Deal Load (1.0ms)  SELECT "deals".* FROM "deals"
=> #<ActiveRecord::QueryMethods::WhereChain:0x00007fbc61228038
 @scope=
  [#<Deal:0x00007fbc5fdb2400
    id: 7,

我想如果我知道如何使用“正常” where語句進行比較(而不是相等),例如where(end_date>Date.today)可以幫助解決我的問題,因為范圍方法使用簡單的where語句,如scope :posted, -> { where.not(posted_date:nil) }返回一個正常可用的ActiveRecord.Relation ,但我確實想弄清楚如何正確使用更復雜的查詢。

你可以使用如下:

scope :current, -> { where('end_date > ?', Date.today) }

Rails中不存在where -call的塊語法。

您還可以查看arelhttps://robots.thoughtbot.com/using-arel-to-compose-sql-queries );

deal = Deal.arel_table
Deal.where(deal[:end_date].gt(Date.today))

未定義的方法“訂單”# <activerecord::querymethods::wherechain< div><div id="text_translate"><p> 我已經在一個名為 Ctrlpanlel 的程序上工作了一段時間,我在 Heroku 上安裝了它,它正在工作……主要是只有少數例外。 作為背景,它曾經在 Ruby 2.2.2 和 Rails 4.2.1 上運行,我已將其升級到 Ruby 2.7.2 和 Rails 6.1.3.2</p><p> 這個應用程序的功能之一是每周費用報告,我遇到了一些查詢邏輯的問題,因為它最初是使用名為 squeel 的 gem 編寫的。 我對 Rails 和 Activerecord 非常陌生,盡管我正在學習我正在嘗試在一些地方取消代碼。 我相信我已經修復了前幾個查詢(我認為至少)並且它超越了我遇到錯誤的前幾個部分,我可以通過運行 rails week_expense_reports 來運行任務,它會使用沒有問題,並顯示了人員和他們每個人的收費數量,但隨后它還為每個人說明了以下錯誤:</p><blockquote><p> 錯誤處理史密斯約翰,因為:#ActiveRecord::QueryMethods::WhereChain:0x000000001067ee08 的未定義方法“訂單”</p></blockquote><p> 為了清楚起見,我將員工姓名更改為 John Smith。 :)</p><p> 它為每個人執行此操作,因此我知道我認為我正確修復的第一個查詢我沒有正確修復,或者 controller 中存在另一個問題,其中包含 .order 行,這就是它所指的。 我了解到,有時在 Rails 中,當它指向一個錯誤的方法時,有時問題是提供方法的數據而不一定是方法本身。 在這種情況下,我不確定它是哪一個。</p><p> 我更改的第一行是:</p><pre class="lang-rb prettyprint-override"> @users = User.where.not(:expense_reports_to_user_id =&gt; nil)</pre><p> 我更改的第二行是:</p><pre class="lang-rb prettyprint-override"> charge_count = Charge.between_times( start_date, end_date, field: :post_date ). where(:cardholder_name =&gt; user.cardholder_name).count</pre><p> 這是 scheduler.rake 中的任務</p><pre>task:weekly_expense_reports =&gt;:environment do if Time.zone.now.wday == 2 log = [] # Squeel logic I had to de-squeel v0.0040 on 5/10/2021 Scott Milella # @users = User.where{ expense_reports_to_user_id.not_eq nil } @users = User.where.not(:expense_reports_to_user_id =&gt; nil) # &lt;- First Line I changed message = "Evaluating #{@users.count} users" puts message log &lt;&lt; message start_date = Time.zone.today - 7.days end_date = Time.zone.today - 1.days if Charge.between_times( start_date, end_date, field: :post_date ).any? date_ranges = [] if start_date.month == end_date.month date_ranges &lt;&lt; { start_date: start_date, end_date: end_date } else date_ranges &lt;&lt; { start_date: start_date, end_date: start_date.end_of_month } date_ranges &lt;&lt; { start_date: end_date.beginning_of_month, end_date: end_date } end message = "Found charges in date ranges: #{date_ranges.join(" to ")}" puts message log &lt;&lt; message @users.each do |user| puts "Checking #{user.name} for charges..." # Below here is the next query I changed... # More squeel code yet again, v0.0040 on 5/10/2021 Scott Milella # charge_count = Charge.between_times( start_date, end_date, field: :post_date ). # where{ cardholder_name.eq my{ user.cardholder_name } }.count charge_count = Charge.between_times( start_date, end_date, field: :post_date ). where(:cardholder_name =&gt; user.cardholder_name).count if charge_count &gt; 0 message = "Found #{charge_count} charges for #{user.name}" puts message log &lt;&lt; message begin ExpenseMailer.weekly_report(user, date_ranges).deliver_now. sleep 1 message = "Successfully emailed charges for #{user.name}" puts message log &lt;&lt; message rescue Exception =&gt; error message = "ERROR processing #{user:name} because, #{error}" puts message log &lt;&lt; message end end end # finished processing emails: send report log &lt;&lt; "Finished Processing Expense Reports" ActionMailer:.Base:mail( from. "tech@des,direct": to. "terrym@des,direct": subject, "Expense Report Summary": body. log.join("\n\n").html_safe ),deliver_now else # no charges found. send notification emails = ["terrym@des,direct". "laurah@des,direct"] puts "No charges were found. skipping expense report:" ActionMailer:.Base:mail( from. "tech@des,direct": to, emails: subject, "[Expense Reports] No Charges Found, Cannot Process": body, "Error, no charges were uploaded for the period of #{start_date} to #{end_date}. please reprocess manually." ).deliver_now end log = nil end end</pre><p> 這是 model 以防萬一:</p><pre> class Charge &lt; ActiveRecord::Base attr_accessible:card_last_four, :cardholder_name, :cardholder_last_name, :cardholder_origin, :post_date, :merchant_name, :amount, :trans_type, :gl_code, :mcc_code, :mcc_description, :gas_region, :transaction_date before_save:add_cardholder_last_name def add_cardholder_last_name if cardholder_name self.cardholder_last_name = cardholder_name.split(" ").last end end def self.allowed_user_ids administration_user_ids end def self.administration_user_ids [ 8, 9, 36, 50, 128 # ] # this includes (not in order) end def update_gl_code self.gl_code = Charge.mcc_to_gl_table[mcc_code.to_i] self.save end def self.mcc_to_gl_table { 0 =&gt; 740, } end def self.cardholder_name_select Charge.pluck(:cardholder_name). uniq. delete_if{ |x| x.in?( Charge.cardholder_reject_list ) }. reject(&amp;:blank?). sort_by{ |k| k.split(" ").last } end def self.cardholder_reject_list [ "JOHN DOE", "JOHN DOE", "JOHN DOE", "JOHN DOE", ] end def self.cardholder_name_to_details { "ACCOUNTS PAYABLE" =&gt; { branch: "redding", region: "TRAVEL" }, } end def self.import_csv_text(csv_text) require 'csv' csv = CSV.parse( csv_text, headers: true ) csv.each do |raw| row = raw.to_hash merchant_name = row["Merchant Name"].strip if merchant_name == "PAYMENT - THANK YOU" card_last_four = 0 cardholder_name = "Payment" else if row["Originating Account Number"].present? card_last_four = row["Originating Account Number"][-4, 4] cardholder_name = row["Cardholder Name"].strip else cardholder_name = "ACCOUNTS PAYABLE" card_last_four = 1234 end end transaction_date = Chronic.parse( row["Tran Date"] ).to_date post_date = Chronic.parse( row["Post Date"] ).to_date amount = row["Amount"].remove('$') trans_type = row["Tran Type"].strip mcc_code = row["MCC Code"].to_i mcc_description = row["MCC Description"].strip reference_number = row["Reference Number"].strip gl_code = Charge.mcc_to_gl_table[ mcc_code ] cardholder_details = Charge.cardholder_name_to_details[ cardholder_name ] if cardholder_details cardholder_origin = cardholder_details[:branch] gas_region = cardholder_details[:region] end @charge = Charge.where(reference_number: reference_number, amount: amount).first_or_create @charge.card_last_four = card_last_four @charge.transaction_date = transaction_date @charge.post_date = post_date @charge.merchant_name = merchant_name @charge.cardholder_name = cardholder_name @charge.trans_type = trans_type @charge.mcc_code = mcc_code unless @charge.mcc_code.present? @charge.mcc_description = mcc_description unless @charge.mcc_description.present? @charge.gl_code = gl_code unless @charge.gl_code.present? @charge.cardholder_origin = cardholder_origin unless @charge.cardholder_origin.present? @charge.gas_region = gas_region unless @charge.gas_region.present? @charge.save end end end</pre><p> 這是 rails 面板中的完整錯誤,其中員工姓名已更改:</p><pre> From: tech@email.domain Subject: Expense Report Summary Date: May 11, 2021 03:41:12 PM Pacific Daylight Time To: Tech@email.domain Evaluating 38 users Found charges in date ranges: {:start_date=&gt;Tue, 04 May 2021, :end_date=&gt;Mon, 10 May 2021} Found 10 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000cc282e0&gt; Found 3 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x0000000009970410&gt; Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x00000000106fc830&gt; Finished Processing Expense Reports</pre><p> 我感謝任何人提供的任何建議、建議和解決方案!</p><p> 謝謝你,</p><p> 斯科特</p><p>這是來自 rails 面板的復制海峽,這是我所知道的手動觸發此腳本的唯一方法。 通常它會從 Heroku 調度程序調用。</p><pre> scottm@RED-IT-LAP-0001 MINGW64 /d/rails/ctrlpanel (master) $ rake weekly_expense_reports Evaluating 38 users Found charges in date ranges: {:start_date=&gt;Wed, 05 May 2021, :end_date=&gt;Tue, 11 May 2021} Checking John Doe for charges... Found 8 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f06fdb0&gt; Checking John Doe for charges... Found 2 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f3bec78&gt; Checking John Doe for charges... Checking John Doe for charges... Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000dfc0a50&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f5e1fa0&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f653420&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000dbfbe38&gt; Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f70ac38&gt; Checking John Doe for charges... Checking John Doe for charges... Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x0000000010614328&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x00000000106cc810&gt; Checking John Doe for charges... Found 3 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000001072c6c0&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000001078d2e0&gt; scottm@RED-IT-LAP-0001 MINGW64 /d/rails/ctrlpanel (master) $</pre><p> 正如您在上面看到的,它幾乎給了我與 email 上的信息完全相同的信息,但這不是很有幫助。 抱歉,我提供了太多代碼,我以為我只提供了圍繞問題的必要代碼,人們通常會問我更多。</p><p> 它引用的有問題的行在charges_controller.rb中,但我擔心我修改的查詢(可能是錯誤的)可能是它說該方法未定義的原因,因為controller邏輯對我來說還可以嗎? 但話又說回來,我很新,仍然在努力理解 Rails 邏輯。 來自控件的方法如下:</p><pre> def index if current_user.id.in?( Charge.administration_user_ids ) || current_user.cardholder_name.present? params[:q] = {} if params[:q].blank? params[:q][:post_date_gteq] = Date.today.beginning_of_month - 1.month if params[:q][:post_date_gteq].blank? # pre-set the cardholder name if they've been assigned # unless they're an administrator if current_user.cardholder_name.present? &amp;&amp;.current_user.id?in.( Charge:administration_user_ids ) params[.q]["cardholder_name_eq"] = current_user.cardholder_name end @q = Charge:ransack(params[,q]) # done too quickly: seriously needs rework if params[:q][.cardholder_name_eq]?present. if current_user.id?in.( Charge.administration_user_ids ) @charges = @q.result:order(,gl_code: .post_date):group_by(&amp;.gl_code) else @charges = @q.result:order(,cardholder_name: .post_date) end else @charges = @q.result:order(,cardholder_name: ,post_date) end else redirect_to root_path: notice. "Nope." end end</pre><p> 為了清楚起見,我將所有員工姓名重命名為 John Doe。 :)</p><p> 如果您還有其他需要,請告訴我,再次感謝您! 斯科特</p><p>我刪除了日志文件,因為它以不同的方式明確表示它幾乎是相同的信息。</p><p> 這是 app/mailers/expense_mailer.rb 中的文件</p><pre>class ExpenseMailer &lt; ApplicationMailer def weekly_report(user, date_ranges) require 'prawn' date_ranges.each do | range | start_date = range[:start_date ] end_date = range[:end_date ] @charges = Charge.between_times( start_date, end_date, field: :post_date ). where{cardholder_name.eq my { user.cardholder_name } }. order("post_date asc") if @charges.any? prawn_output = ExpenseReport.new(user.cardholder_name, start_date, end_date, @charges).render prawn_object = CombinePDF.parse(prawn_output, page_layout: :landscape) combined_file = CombinePDF.new template_path = 'app/assets/pdfs/DES_Expense_Report_W4.pdf' template_page = CombinePDF.load( template_path, page_layout: :landscape ).pages[0]; true prawn_object.pages.each do |page| template_page = CombinePDF.load( template_path, page_layout: :landscape ).pages[0]; true template_page[:Annots].each { |a| a[:referenced_object][:T] = 'Combine' + SecureRandom.hex(7) + 'PDF' } combined_file &lt;&lt; template_page end prawn_object.pages.each_with_index do |page, i| combined_file.pages[i] &lt;&lt; prawn_object.pages[i] end page_total = combined_file.pages.count combined_file.pages.each_with_index do |page, i| page.textbox( "Page #{i + 1}/#{page_total}", x: 680, y: -520, max_font_size: 10 ) end attachments["#{user.cardholder_name} [ #{start_date.strftime("%m/%d/%Y")} - #{end_date.strftime("%m/%d/%Y")} ].pdf"] = combined_file.to_pdf end end to_with_name = %("#{user.expense_report_manager.name}" &lt;#{user.expense_report_manager.email}&gt;) # to_with_name = %("John" &lt;johnd@mail.domain&gt;) from_with_name = %("CtrlPanel" &lt;tech@mail.domain&gt;) mail to: to_with_name, bcc: 'tech@mail.domain from: from_with_name, subject: "Expense Report for #{user.name} [ #{date_ranges.first[:start_date].strftime("%m/%d/%Y")} - #{date_ranges.last[:end_date].strftime("%m/%d/%Y")} ]" end end</pre><p> 謝謝你,斯科特</p></div></activerecord::querymethods::wherechain<>

[英]Undefined method `order' for #<ActiveRecord::QueryMethods::WhereChain

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Rails 3:范圍返回一個數組 Rails Scope返回all而不是nil Rails:方法返回對象而不是屬性 <ActiveRecord::QueryMethods::WhereChain error on a scope that was working Rails render json返回數組而不是字典 Rails 查詢返回數組而不是 ActiveRecord 如何在 Rails 6 中查詢 WhereChain? 未定義的方法“訂單”# <activerecord::querymethods::wherechain< div><div id="text_translate"><p> 我已經在一個名為 Ctrlpanlel 的程序上工作了一段時間,我在 Heroku 上安裝了它,它正在工作……主要是只有少數例外。 作為背景,它曾經在 Ruby 2.2.2 和 Rails 4.2.1 上運行,我已將其升級到 Ruby 2.7.2 和 Rails 6.1.3.2</p><p> 這個應用程序的功能之一是每周費用報告,我遇到了一些查詢邏輯的問題,因為它最初是使用名為 squeel 的 gem 編寫的。 我對 Rails 和 Activerecord 非常陌生,盡管我正在學習我正在嘗試在一些地方取消代碼。 我相信我已經修復了前幾個查詢(我認為至少)並且它超越了我遇到錯誤的前幾個部分,我可以通過運行 rails week_expense_reports 來運行任務,它會使用沒有問題,並顯示了人員和他們每個人的收費數量,但隨后它還為每個人說明了以下錯誤:</p><blockquote><p> 錯誤處理史密斯約翰,因為:#ActiveRecord::QueryMethods::WhereChain:0x000000001067ee08 的未定義方法“訂單”</p></blockquote><p> 為了清楚起見,我將員工姓名更改為 John Smith。 :)</p><p> 它為每個人執行此操作,因此我知道我認為我正確修復的第一個查詢我沒有正確修復,或者 controller 中存在另一個問題,其中包含 .order 行,這就是它所指的。 我了解到,有時在 Rails 中,當它指向一個錯誤的方法時,有時問題是提供方法的數據而不一定是方法本身。 在這種情況下,我不確定它是哪一個。</p><p> 我更改的第一行是:</p><pre class="lang-rb prettyprint-override"> @users = User.where.not(:expense_reports_to_user_id =&gt; nil)</pre><p> 我更改的第二行是:</p><pre class="lang-rb prettyprint-override"> charge_count = Charge.between_times( start_date, end_date, field: :post_date ). where(:cardholder_name =&gt; user.cardholder_name).count</pre><p> 這是 scheduler.rake 中的任務</p><pre>task:weekly_expense_reports =&gt;:environment do if Time.zone.now.wday == 2 log = [] # Squeel logic I had to de-squeel v0.0040 on 5/10/2021 Scott Milella # @users = User.where{ expense_reports_to_user_id.not_eq nil } @users = User.where.not(:expense_reports_to_user_id =&gt; nil) # &lt;- First Line I changed message = "Evaluating #{@users.count} users" puts message log &lt;&lt; message start_date = Time.zone.today - 7.days end_date = Time.zone.today - 1.days if Charge.between_times( start_date, end_date, field: :post_date ).any? date_ranges = [] if start_date.month == end_date.month date_ranges &lt;&lt; { start_date: start_date, end_date: end_date } else date_ranges &lt;&lt; { start_date: start_date, end_date: start_date.end_of_month } date_ranges &lt;&lt; { start_date: end_date.beginning_of_month, end_date: end_date } end message = "Found charges in date ranges: #{date_ranges.join(" to ")}" puts message log &lt;&lt; message @users.each do |user| puts "Checking #{user.name} for charges..." # Below here is the next query I changed... # More squeel code yet again, v0.0040 on 5/10/2021 Scott Milella # charge_count = Charge.between_times( start_date, end_date, field: :post_date ). # where{ cardholder_name.eq my{ user.cardholder_name } }.count charge_count = Charge.between_times( start_date, end_date, field: :post_date ). where(:cardholder_name =&gt; user.cardholder_name).count if charge_count &gt; 0 message = "Found #{charge_count} charges for #{user.name}" puts message log &lt;&lt; message begin ExpenseMailer.weekly_report(user, date_ranges).deliver_now. sleep 1 message = "Successfully emailed charges for #{user.name}" puts message log &lt;&lt; message rescue Exception =&gt; error message = "ERROR processing #{user:name} because, #{error}" puts message log &lt;&lt; message end end end # finished processing emails: send report log &lt;&lt; "Finished Processing Expense Reports" ActionMailer:.Base:mail( from. "tech@des,direct": to. "terrym@des,direct": subject, "Expense Report Summary": body. log.join("\n\n").html_safe ),deliver_now else # no charges found. send notification emails = ["terrym@des,direct". "laurah@des,direct"] puts "No charges were found. skipping expense report:" ActionMailer:.Base:mail( from. "tech@des,direct": to, emails: subject, "[Expense Reports] No Charges Found, Cannot Process": body, "Error, no charges were uploaded for the period of #{start_date} to #{end_date}. please reprocess manually." ).deliver_now end log = nil end end</pre><p> 這是 model 以防萬一:</p><pre> class Charge &lt; ActiveRecord::Base attr_accessible:card_last_four, :cardholder_name, :cardholder_last_name, :cardholder_origin, :post_date, :merchant_name, :amount, :trans_type, :gl_code, :mcc_code, :mcc_description, :gas_region, :transaction_date before_save:add_cardholder_last_name def add_cardholder_last_name if cardholder_name self.cardholder_last_name = cardholder_name.split(" ").last end end def self.allowed_user_ids administration_user_ids end def self.administration_user_ids [ 8, 9, 36, 50, 128 # ] # this includes (not in order) end def update_gl_code self.gl_code = Charge.mcc_to_gl_table[mcc_code.to_i] self.save end def self.mcc_to_gl_table { 0 =&gt; 740, } end def self.cardholder_name_select Charge.pluck(:cardholder_name). uniq. delete_if{ |x| x.in?( Charge.cardholder_reject_list ) }. reject(&amp;:blank?). sort_by{ |k| k.split(" ").last } end def self.cardholder_reject_list [ "JOHN DOE", "JOHN DOE", "JOHN DOE", "JOHN DOE", ] end def self.cardholder_name_to_details { "ACCOUNTS PAYABLE" =&gt; { branch: "redding", region: "TRAVEL" }, } end def self.import_csv_text(csv_text) require 'csv' csv = CSV.parse( csv_text, headers: true ) csv.each do |raw| row = raw.to_hash merchant_name = row["Merchant Name"].strip if merchant_name == "PAYMENT - THANK YOU" card_last_four = 0 cardholder_name = "Payment" else if row["Originating Account Number"].present? card_last_four = row["Originating Account Number"][-4, 4] cardholder_name = row["Cardholder Name"].strip else cardholder_name = "ACCOUNTS PAYABLE" card_last_four = 1234 end end transaction_date = Chronic.parse( row["Tran Date"] ).to_date post_date = Chronic.parse( row["Post Date"] ).to_date amount = row["Amount"].remove('$') trans_type = row["Tran Type"].strip mcc_code = row["MCC Code"].to_i mcc_description = row["MCC Description"].strip reference_number = row["Reference Number"].strip gl_code = Charge.mcc_to_gl_table[ mcc_code ] cardholder_details = Charge.cardholder_name_to_details[ cardholder_name ] if cardholder_details cardholder_origin = cardholder_details[:branch] gas_region = cardholder_details[:region] end @charge = Charge.where(reference_number: reference_number, amount: amount).first_or_create @charge.card_last_four = card_last_four @charge.transaction_date = transaction_date @charge.post_date = post_date @charge.merchant_name = merchant_name @charge.cardholder_name = cardholder_name @charge.trans_type = trans_type @charge.mcc_code = mcc_code unless @charge.mcc_code.present? @charge.mcc_description = mcc_description unless @charge.mcc_description.present? @charge.gl_code = gl_code unless @charge.gl_code.present? @charge.cardholder_origin = cardholder_origin unless @charge.cardholder_origin.present? @charge.gas_region = gas_region unless @charge.gas_region.present? @charge.save end end end</pre><p> 這是 rails 面板中的完整錯誤,其中員工姓名已更改:</p><pre> From: tech@email.domain Subject: Expense Report Summary Date: May 11, 2021 03:41:12 PM Pacific Daylight Time To: Tech@email.domain Evaluating 38 users Found charges in date ranges: {:start_date=&gt;Tue, 04 May 2021, :end_date=&gt;Mon, 10 May 2021} Found 10 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000cc282e0&gt; Found 3 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x0000000009970410&gt; Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x00000000106fc830&gt; Finished Processing Expense Reports</pre><p> 我感謝任何人提供的任何建議、建議和解決方案!</p><p> 謝謝你,</p><p> 斯科特</p><p>這是來自 rails 面板的復制海峽,這是我所知道的手動觸發此腳本的唯一方法。 通常它會從 Heroku 調度程序調用。</p><pre> scottm@RED-IT-LAP-0001 MINGW64 /d/rails/ctrlpanel (master) $ rake weekly_expense_reports Evaluating 38 users Found charges in date ranges: {:start_date=&gt;Wed, 05 May 2021, :end_date=&gt;Tue, 11 May 2021} Checking John Doe for charges... Found 8 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f06fdb0&gt; Checking John Doe for charges... Found 2 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f3bec78&gt; Checking John Doe for charges... Checking John Doe for charges... Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000dfc0a50&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f5e1fa0&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f653420&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000dbfbe38&gt; Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000000f70ac38&gt; Checking John Doe for charges... Checking John Doe for charges... Found 5 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x0000000010614328&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x00000000106cc810&gt; Checking John Doe for charges... Found 3 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000001072c6c0&gt; Checking John Doe for charges... Found 1 charges for John Doe ERROR processing John Doe because: undefined method `order' for #&lt;ActiveRecord::QueryMethods::WhereChain:0x000000001078d2e0&gt; scottm@RED-IT-LAP-0001 MINGW64 /d/rails/ctrlpanel (master) $</pre><p> 正如您在上面看到的,它幾乎給了我與 email 上的信息完全相同的信息,但這不是很有幫助。 抱歉,我提供了太多代碼,我以為我只提供了圍繞問題的必要代碼,人們通常會問我更多。</p><p> 它引用的有問題的行在charges_controller.rb中,但我擔心我修改的查詢(可能是錯誤的)可能是它說該方法未定義的原因,因為controller邏輯對我來說還可以嗎? 但話又說回來,我很新,仍然在努力理解 Rails 邏輯。 來自控件的方法如下:</p><pre> def index if current_user.id.in?( Charge.administration_user_ids ) || current_user.cardholder_name.present? params[:q] = {} if params[:q].blank? params[:q][:post_date_gteq] = Date.today.beginning_of_month - 1.month if params[:q][:post_date_gteq].blank? # pre-set the cardholder name if they've been assigned # unless they're an administrator if current_user.cardholder_name.present? &amp;&amp;.current_user.id?in.( Charge:administration_user_ids ) params[.q]["cardholder_name_eq"] = current_user.cardholder_name end @q = Charge:ransack(params[,q]) # done too quickly: seriously needs rework if params[:q][.cardholder_name_eq]?present. if current_user.id?in.( Charge.administration_user_ids ) @charges = @q.result:order(,gl_code: .post_date):group_by(&amp;.gl_code) else @charges = @q.result:order(,cardholder_name: .post_date) end else @charges = @q.result:order(,cardholder_name: ,post_date) end else redirect_to root_path: notice. "Nope." end end</pre><p> 為了清楚起見,我將所有員工姓名重命名為 John Doe。 :)</p><p> 如果您還有其他需要,請告訴我,再次感謝您! 斯科特</p><p>我刪除了日志文件,因為它以不同的方式明確表示它幾乎是相同的信息。</p><p> 這是 app/mailers/expense_mailer.rb 中的文件</p><pre>class ExpenseMailer &lt; ApplicationMailer def weekly_report(user, date_ranges) require 'prawn' date_ranges.each do | range | start_date = range[:start_date ] end_date = range[:end_date ] @charges = Charge.between_times( start_date, end_date, field: :post_date ). where{cardholder_name.eq my { user.cardholder_name } }. order("post_date asc") if @charges.any? prawn_output = ExpenseReport.new(user.cardholder_name, start_date, end_date, @charges).render prawn_object = CombinePDF.parse(prawn_output, page_layout: :landscape) combined_file = CombinePDF.new template_path = 'app/assets/pdfs/DES_Expense_Report_W4.pdf' template_page = CombinePDF.load( template_path, page_layout: :landscape ).pages[0]; true prawn_object.pages.each do |page| template_page = CombinePDF.load( template_path, page_layout: :landscape ).pages[0]; true template_page[:Annots].each { |a| a[:referenced_object][:T] = 'Combine' + SecureRandom.hex(7) + 'PDF' } combined_file &lt;&lt; template_page end prawn_object.pages.each_with_index do |page, i| combined_file.pages[i] &lt;&lt; prawn_object.pages[i] end page_total = combined_file.pages.count combined_file.pages.each_with_index do |page, i| page.textbox( "Page #{i + 1}/#{page_total}", x: 680, y: -520, max_font_size: 10 ) end attachments["#{user.cardholder_name} [ #{start_date.strftime("%m/%d/%Y")} - #{end_date.strftime("%m/%d/%Y")} ].pdf"] = combined_file.to_pdf end end to_with_name = %("#{user.expense_report_manager.name}" &lt;#{user.expense_report_manager.email}&gt;) # to_with_name = %("John" &lt;johnd@mail.domain&gt;) from_with_name = %("CtrlPanel" &lt;tech@mail.domain&gt;) mail to: to_with_name, bcc: 'tech@mail.domain from: from_with_name, subject: "Expense Report for #{user.name} [ #{date_ranges.first[:start_date].strftime("%m/%d/%Y")} - #{date_ranges.last[:end_date].strftime("%m/%d/%Y")} ]" end end</pre><p> 謝謝你,斯科特</p></div></activerecord::querymethods::wherechain<> Rails范圍返回重復項 Rails Scope返回一個數組,而不是ActiveRecord_Relation
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM