Ruby on rails 在rails 3.2.12中找不到没有ID的模型
我喜欢这种方法。我一点也不能理解这个错误Ruby on rails 在rails 3.2.12中找不到没有ID的模型,ruby-on-rails,associations,Ruby On Rails,Associations,我喜欢这种方法。我一点也不能理解这个错误 Couldn't find Company without an ID 在ActiveRecord::RecordNotFound in CustomerController#批量创建中 编写此方法是为了批量为公司创建客户,方法是以name:number格式记录客户的姓名和号码 方法如下: def bulk_create res = "" comp_id = params[:customer][:selected_companies].delete_i
Couldn't find Company without an ID
在ActiveRecord::RecordNotFound in CustomerController#批量创建中
编写此方法是为了批量为公司创建客户,方法是以name:number
格式记录客户的姓名和号码
方法如下:
def bulk_create
res = ""
comp_id = params[:customer][:selected_companies].delete_if{|a| a.blank?}.first
comp = Company.find(comp_id)
s = SentSmsMessage.new
s.set_defaults
s.data = tmpl("command_signup_ok", customer, comp) unless params[:customer][:email].length > 0
s.data = params[:customer][:email] if params[:customer][:email].length > 0
s.company = comp if !comp.nil?
s.save
unless comp_id.blank?
params[:customer][:name].lines.each do |line|
(name, phone) = line.split(/\t/) unless line.include?(":")
(name, phone) = line.split(":") if line.include?(":")
phone = phone.gsub("\"", "")
phone = phone.strip if phone.strip.to_i > 0
name = name.gsub("\"", "")
name = name.gsub("+", "")
phone = "47#{phone}" if params[:customer][:active].to_i == 1
customer = Customer.first(:conditions => ["phone_number = ?", phone])
if customer.nil?
customer = Customer.new
customer.name = name
# customer.email
# customer.login
# customer.password
customer.accepted_agreement = DateTime.now
customer.phone_number = phone
customer.active = true
customer.accepted_agreement = DateTime.now
customer.max_msg_week = params[:customer][:max_msg_week]
customer.max_msg_day = params[:customer][:max_msg_day]
customer.selected_companies = params[:customer][:selected_companies].delete_if{|a| a.blank?}
res += "#{name} - #{phone}: Create OK<br />" if customer.save
res += "#{name} - #{phone}: Create failed<br />" unless customer.save
else
params[:customer][:selected_companies].each do |cid|
new_company = Company.find(cid) unless cid.blank?
if !new_company.nil?
if !customer.companies.include?(new_company)
customer.companies << new_company
if customer.save
res += "#{name} - #{phone}: Customer exists and the customer was added to the firm #{new_company.name}<br />"
else
res += "#{name} - #{phone}: Customer exist, but something went wrong during storage. Check if the client is in the firm.<br />"
end
else
res += "#{name} - #{phone}: Customer exists and is already on firm #{new_company.name}<br />"
end
end
end
end
s.sms_recipients.create(:phone_number => customer.phone_number)
end
s.save
s.send_as_sms
@result = res
respond_to do |format|
format.html { render "bulk_create"}
end
else
@result = "You have not selected any firm to add these users. Press the back button and try again."
respond_to do |format|
format.html { render "bulk_create"}
end
end
end
从模型customer.rb
def selected_companies=(cmps)
cmps.delete("")
# Check the old ones. Make a note if they are not in the list. If the existing ones are not in the new list, just remove them
self.companies.each do |c|
self.offer_subscriptions.find(:first, ["customer_id = ?", c]).destroy unless cmps.include? c.id.to_s
cmps.delete c.id.to_s if cmps.include? c.id.to_s
end
# Then create the new ones
cmps.each do |c2|
cmp = Company.find(:first, ["id = ?", c2])
if cmp && !c2.blank?
offerSubs = offer_subscriptions.new
offerSubs.company_id = c2
offerSubs.save
end
end
end
def selected_companies
return self.companies.collect{|c| c.id}
end
客户的关联如下:
def bulk_create
res = ""
comp_id = params[:customer][:selected_companies].delete_if{|a| a.blank?}.first
comp = Company.find(comp_id)
s = SentSmsMessage.new
s.set_defaults
s.data = tmpl("command_signup_ok", customer, comp) unless params[:customer][:email].length > 0
s.data = params[:customer][:email] if params[:customer][:email].length > 0
s.company = comp if !comp.nil?
s.save
unless comp_id.blank?
params[:customer][:name].lines.each do |line|
(name, phone) = line.split(/\t/) unless line.include?(":")
(name, phone) = line.split(":") if line.include?(":")
phone = phone.gsub("\"", "")
phone = phone.strip if phone.strip.to_i > 0
name = name.gsub("\"", "")
name = name.gsub("+", "")
phone = "47#{phone}" if params[:customer][:active].to_i == 1
customer = Customer.first(:conditions => ["phone_number = ?", phone])
if customer.nil?
customer = Customer.new
customer.name = name
# customer.email
# customer.login
# customer.password
customer.accepted_agreement = DateTime.now
customer.phone_number = phone
customer.active = true
customer.accepted_agreement = DateTime.now
customer.max_msg_week = params[:customer][:max_msg_week]
customer.max_msg_day = params[:customer][:max_msg_day]
customer.selected_companies = params[:customer][:selected_companies].delete_if{|a| a.blank?}
res += "#{name} - #{phone}: Create OK<br />" if customer.save
res += "#{name} - #{phone}: Create failed<br />" unless customer.save
else
params[:customer][:selected_companies].each do |cid|
new_company = Company.find(cid) unless cid.blank?
if !new_company.nil?
if !customer.companies.include?(new_company)
customer.companies << new_company
if customer.save
res += "#{name} - #{phone}: Customer exists and the customer was added to the firm #{new_company.name}<br />"
else
res += "#{name} - #{phone}: Customer exist, but something went wrong during storage. Check if the client is in the firm.<br />"
end
else
res += "#{name} - #{phone}: Customer exists and is already on firm #{new_company.name}<br />"
end
end
end
end
s.sms_recipients.create(:phone_number => customer.phone_number)
end
s.save
s.send_as_sms
@result = res
respond_to do |format|
format.html { render "bulk_create"}
end
else
@result = "You have not selected any firm to add these users. Press the back button and try again."
respond_to do |format|
format.html { render "bulk_create"}
end
end
end
has_many :offer_subscriptions
has_many :companies, :through => :offer_subscriptions
这段代码是由其他人编写的。我试图理解这个方法,但到目前为止还不能理解这个代码。
请帮忙。
提前感谢。您将收到“找不到没有ID的公司”错误,因为您的公司表不包含ID=comp\u ID的记录 将
comp=Company.find(comp\u id)
更改为comp=Company.find by\u id(comp\u id)
这将返回nil而不是错误
添加
comp不是nil
条件已在您的代码中处理。您的comp\u id行返回nil
comp_id = params[:customer][:selected_companies].delete_if{|a| a.blank?}.first
Reason being
params[:customer][:selected_companies].delete_if{|a| a.blank?} = []
so [].first = nil
therefor, params[:customer][:selected_companies].delete_if{|a| a.blank?}.first = nil
and comp_id is nil
So check the log file and check what is coming in the parameter "selected_companies"
when you will find the parameter, everything will be understood well....
发布传递给这个函数的参数,我们希望能找到原因。同时,您可以在begin-rescue
块中包含该块,以捕获以下错误:
begin
<all your code>
rescue ActiveRecord::RecordNotFound
return 'Unable to find a matching record'
end
开始
rescue ActiveRecord::RecordNotFound
返回“找不到匹配的记录”
结束
试试这个:
comp = ""
comp = Company.find(comp_id) unless comp_id.nil?
而不是comp=Company.find(comp\u id)
进一步
nil
检查代码中是否存在。参数[:客户][:所选公司]。如果{a | a.blank?}删除。首先调试并评估此参数。我想它归零了谢谢rony。。我有点新手。你能告诉我怎么做吗?你用的是哪个IDE?我建议您使用JetBrain RubyMine。并按照说明进行调试。我想你指的是文本编辑器?我正在使用text mate
。但是您无法使用文本编辑器调试应用程序。正在引发ActiveRecord::RecordNotFound
,因为没有ID传递给查找,而不是没有匹配的记录。因为find\u by\u ID
方法已被弃用。所以在这里发布之前,我也尝试了where
。但是不能成功。现在,在我做了更改之后,它给出了错误。你告诉我要做的是undefined local variable或method
customer',在这一行s.data=tmpl(“command\u signup\u ok”,customer,comp),除非参数[:customer][:email]。长度>0
你在这一行“s.data=tmpl”(“command\u signup\u ok”,customer,comp)得到了错误除非params[:customer][:email].length>0”,因为没有定义传递给方法tmpl的参数customer。定义了方法,但没有定义传递给方法的参数“customer”。这根本不起作用。Define customer=“您希望在方法tmpl中传递的参数customer的预期值,例如customer=customer.first”在名为tmpl的方法上方。实际上,我不知道如何找到参数的值。我想您询问这个参数:{“utf8”=>”✓", "真实性令牌“=>”I/hdXjPtO+n3entjk8pshj4vmcnhasgtzfgjwboquew=“,”客户“=>{”选定的公司“=>”,“1“,”名称“=>”hamza:9021480192\r\nnivedita:8600438990”,“活动”=>“0”,“电子邮件”=>“你好吗?”,“每周最长消息”=>“5”,“最长消息日”=>“2”,“承诺”=>“Lagre Kunder”}<代码> >选择的公司>代码>为空白。第一个值为空,但您的行删除空白,返回非空白,然后使用第一个值。我粘贴了模型customer.rb
中的selected_companys
方法。这可能会有所帮助。参数:{“utf8”=>“✓", "真实性令牌“=>”I/hdXjPtO+n3entjk8pshj4vmcnhasgtzfgjwboquew=“,”客户“=>{”选定公司“=>”,”1“,”名称“=>”hamza:9021480192\r\nnivedita:8600438990”,“活动”=>“0”,“电子邮件”=>“嘿,你好吗?”,“每周最长消息”=>“5”,“最长消息日”=>“2”,“提交”=>“Lagre Kunder”
选定公司的第一部分为空。
Reason being
params[:customer][:selected_companies].delete_if{|a| a.blank?} = []
so [].first = nil
therefor, params[:customer][:selected_companies].delete_if{|a| a.blank?}.first = nil
and comp_id is nil
So check the log file and check what is coming in the parameter "selected_companies"
when you will find the parameter, everything will be understood well....