当我调用一个函数时,我得到以下错误日志; 请帮助破译它。
NoMethodError (undefined method `first' for #<Matching:0x0000000875a050>):
app/mailers/matching_mailer.rb:6:in `new_matchings_for_customer'
app/models/matching.rb:133:in `block in create_matchings_from_service'
app/models/matching.rb:126:in `each'
app/models/matching.rb:126:in `create_matchings_from_service'
app/models/matching.rb:30:in `process_matchings_for_service'
app/models/payments/subscription.rb:94:in `find_matchings'
app/models/payments/subscription.rb:85:in `after_create_actions'
app/controllers/contractors/subscriptions_controller.rb:51:in `subscribe'
app/controllers/contractors/subscriptions_controller.rb:19:in `create'
编辑1:
匹配邮件的前几行:
class MatchingMailer < ActionMailer::Base
default from: "\"Estimate My Project\" <info@estimatemyproject.com>"
def new_matchings_for_customer(matchings, customer_id)
@customer = Customer.find(customer_id)
@matchings = Matching.find(matchings)
@category = @matchings.first.emp_request.subcategory.category
unless @customer.email.empty?
mail(to: @customer.email, subject: "#{@category.name} estimate for project in #{@customer.zip_code.county.name}, #{@customer.zip_code.state.code} #{@customer.zip_code.code}")
else
self.message.perform_deliveries = false
end
end
答案 0 :(得分:0)
NoMethodError (undefined method `first' for #<Matching:0x0000000875a050>)
表示first
上没有方法Matching
。
app/mailers/matching_mailer.rb:6:in `new_matchings_for_customer'
表示您尝试在`app / mailers / matching_mailer.rb``的第6行匹配实例上调用方法first
查看第6行中的MatchingMailer
,我们会在first
上看到您致电@matching
的人。 @matching
之前只是设置了一行。请注意,Matching.find
在传入单个id
时返回一条记录,并在传入id
数组时返回一组记录。在这种情况下,您将matchings
作为参数提供给new_matchings_for_customer
方法。
很明显,matchings
参数必须是单个id
。否则@matchings
将返回一个数组,一个数组将响应first
。由于您总是先调用并且从不关心数组中的其他值,因此只需加载一条记录就更有意义了。
将您的MatchingMailer
更改为:
class MatchingMailer < ActionMailer::Base
default from: '"Estimate My Project" <info@estimatemyproject.com>'
def new_matchings_for_customer(matching_id, customer_id)
customer = Customer.find(customer_id)
if customer.email.present?
matching = Matching.find(matching_id)
category = matching.emp_request.subcategory.category
mail(
to: customer.email,
subject: "#{category.name} estimate for project in #{customer.zip_code.county.name}, #{customer.zip_code.state.code} #{customer.zip_code.code}"
)
else
self.message.perform_deliveries = false
end
end
end
确保在调用该方法时只传递一个matching_id
。