【问题标题】:Controller unable to retrieve record控制器无法检索记录
【发布时间】:2014-01-03 18:11:44
【问题描述】:

我的控制器正在搜索一条没有我传递给它的 user_id 的记录。

这是我的控制器:

def update
@untrained = Certificate.find_by_user_id(params[:id])
@untrained.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end

这是错误:

Processing by CertificatesController#update as 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx=", "certificate"=>{"user_id"=>"7"}, "commit"=>"Un-Train"}

证书加载 (0.1ms) SELECT certificates.* FROM certificates WHERE certificates.user_id IS NULL LIMIT 1 在 2ms 内完成 500 内部服务器错误

user_id 作为字符串发送,证书中的 user_id 是一个整数。 为什么我的 SELECT 搜索时 user_id 为 NULL?

【问题讨论】:

  • find_by_* 魔术方法已弃用。使用Certificate.where(user_id: params[:id])

标签: sql ruby-on-rails ruby-on-rails-3 activerecord


【解决方案1】:

注意日志中的"certificate"=>{"user_id"=>"7"},

你需要params[:certificate][:user_id],而不是params[:id]

Certificate.where(user_id: params[:id]).first.update_attributes(attend: "No")

【讨论】:

  • 不确定这是否相关,我收到此错误:NoMethodError (undefined method `update_attributes' for #<:relation:0x00000005001720>):
  • 如果你想使用where而不是find_by,你需要添加一个first来把set的记录变成一个特定的 记录。查看更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多