【问题标题】:Rails error with where clause in update_attributes更新属性中 where 子句的 Rails 错误
【发布时间】:2012-10-07 11:44:53
【问题描述】:

我有一个没有主键的表,其中包含三列 - Player_ID、Season 和 Amount。当我想更新当前表时,出现以下错误消息:ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'salaries.' in 'where clause': UPDATE salaries SET Season = 20192, Amount = 3232.0 其中salaries.`` 为空)

在我的控制器中更新方法:

def new
  @salary = Salary.new
  @players = Player.all
  @title = "Add salary"
end

def create
  @players = Player.all
  @salary = Salary.new(params[:salary])
  if @salary.save
    flash[:success] = "Successfully added salary"
    redirect_to salaries_path
  else
    @title = "Add salary"
    render 'new'
  end
end

def edit
  @player_id = params[:player_id]
  @season = params[:season]
  @salary = Salary.find_by_Player_ID_and_Season(params[:player_id], params[:season])
  @players = Player.all
  @title = "Edit salary"
end

def update
    @players = Player.all
    @player_id = params[:player_id]
    @season = params[:season]
    @salary = Salary.find_by_Player_ID_and_Season(params[:player_id], params[:season])
    if @salary.update_attributes(params[:salary])
       flash[:success] = "Successfully edited salary"
       redirect_to salaries_path
    else
       render 'edit'
    end
  end

_form.html.erb

<%= form_for(@salary, :url => salary_path(:player_id => @player_id, :season => @season)) do |f| %>
  <%= f.collection_select :Player_ID, @players, :ID, :Name %>
  <%= f.text_field :Season %>
  <%= f.text_field :Amount %>
  <%= f.submit "Add" %>
<% end %>

【问题讨论】:

  • 听起来你的工资表没有主键
  • 是的。 A 没有主键。我有三列 - Player_ID、Season 和 Amount。我想编辑数据库中的一些行。这就是我的目标。

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2


【解决方案1】:

Active Record 模型必须有一个主键,否则无法更新或删除行。

如果您真的不想在表中添加 ID 列,可以使用 composite_primary_keys gem(尽管添加主键肯定是阻力最小的路径)

【讨论】:

  • primary key 是问题所在。 create table 迁移存在一些问题。感谢您提出这个问题.. :)
【解决方案2】:

您的表已经有了主键!这是id 属性。 params[:season] ...我认为是params[:salary][:season],对吧? 我认为你应该写成小写的season, player_id, amount。之后,如果仍然有问题,请在此处发布新代码^^

【讨论】:

  • 我不确定我是否理解你。我如何拥有主键 ID?为什么它应该是 params[:salary][:season]?而且属性不是小写的,因为我的列是 Season、Player_ID 和 Amount。
  • "还将添加一个名为 id 的主键列,但是由于这是默认值,我们不需要这样做。"看到这里link params[:salary][:season] 而不是 params[:season] 因为薪水实例的形式。
【解决方案3】:

我知道您的错误的原因,尽管我并没有立即清楚根本原因。您失败的查询是这样的:

ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'salaries.' in 'where clause': UPDATE salaries SET Season = 20192, Amount = 3232.0 WHERE salaries.`` IS NULL)

重要的是:

WHERE salaries.`` IS NULL

如果它是WHERE salaries.foo IS NULLWHERE salaries.bar IS NULL,那将是有一定意义的,但你只是在列名通常是空白的地方。所以我猜 MySQL(出于某种奇怪的原因)将其解释为您正在谈论一个名为salaries(即salaries.salaries)的列,它不存在。

我什至不知道为什么WHERE 子句一开始就在那里。

您的create 操作有效吗?

更新:问题几乎可以肯定是这一行:

if @salary.update_attributes(params[:salary])

我相信你想要的是:

if @salary.update_attributes(:amount => params[:amount])

看看有没有什么作用。

【讨论】:

  • 在我的帖子中添加了新的、创建和编辑方法。我的创建方法有效,但我的形式在那里不同。第一行只有
  • 好的,这是我的建议。首先,使update 表单与create 表单匹配。使update 操作尽可能简单明了,并让它发挥作用。然后,一点一点地调整您的表单和控制器代码以匹配您最初的需求。
  • 在发布这个问题之前我已经做了很多次,但不明白 where 子句的问题在哪里。 where 子句的声明应该是:“where seals.player_id = player_id from url andsalary.season = season from url”
  • 如果我从 _form.html 中删除 ":url => Salary_path(:player_id => @player_id, :season => @season)" 我有一条错误消息:“未定义的方法 `join' for nil:NilClass"
  • 你给我们看的这个控制器是app/controllers/salaries_controller.rb,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多