当一条记录被标记为默认时,如何更新所有其他记录?

发布于 2024-09-27 03:16:48 字数 818 浏览 4 评论 0原文

当有人将其中一个标记为 TRUE 时,我尝试将所有其他记录的所有 default_standing 字段更改为 FALSE。这样我表中就只有一个默认记录。这是我在控制器中的创建和更新中所做的事情,但它似乎不起作用:

def update
    @standing = Standing.find(params[:id])

    if @standing.default_standing
      @standings = Standing.where(["default_standing = ? AND id != ?", true, params[:id]])

      @standings.each do |s|
        s.default_standing = false
        s.save!
      end
    end

    respond_to do |format|
      if @standing.update_attributes(params[:standing])
        format.html { redirect_to(@standing, :notice => 'Standing was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @standing.errors, :status => :unprocessable_entity }
      end
    end
  end

I am trying to change all of the default_standing fields to FALSE for all other records when someone marks one as TRUE. That way I will only ever have one default record in the table. Here is what I am doing in both create and update in my controller, but it doesn't seem to be working:

def update
    @standing = Standing.find(params[:id])

    if @standing.default_standing
      @standings = Standing.where(["default_standing = ? AND id != ?", true, params[:id]])

      @standings.each do |s|
        s.default_standing = false
        s.save!
      end
    end

    respond_to do |format|
      if @standing.update_attributes(params[:standing])
        format.html { redirect_to(@standing, :notice => 'Standing was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @standing.errors, :status => :unprocessable_entity }
      end
    end
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洋洋洒洒 2024-10-04 03:16:48

我认为shingara的update_all中的条件是错误的。
应该更新所有 id 不存在的地方。id:

class Standing

  def self.all_false_instead_of(standing)
    return if standing.default_standing
    Standing.update_all(["default_standing = ?", false], ['id <> ?', standing.id])
    standing.update_attributes!(:default_standing, true)
  end
end

I think the condition is wrong in shingara's update_all.
Should update all where id is not standing.id:

class Standing

  def self.all_false_instead_of(standing)
    return if standing.default_standing
    Standing.update_all(["default_standing = ?", false], ['id <> ?', standing.id])
    standing.update_attributes!(:default_standing, true)
  end
end
云朵有点甜 2024-10-04 03:16:48
class Standing

  def self.all_false_instead_of(standing)
    return if standing.default_standing
    Standing.update_all("default_standing = false", {:id => standing.id})
    standing.update_attributes!(:default_standing, true)
  end
end

我认为模型和类似的东西更好。你的单元测试失败了吗?

在你的控制器中

def update
    @standing = Standing.find(params[:id])
    Standing.all_false_instead_of(@standing)
end

在你的代码中你永远不会将default_standp推到@stand中的true

class Standing

  def self.all_false_instead_of(standing)
    return if standing.default_standing
    Standing.update_all("default_standing = false", {:id => standing.id})
    standing.update_attributes!(:default_standing, true)
  end
end

It's better in Model and something like that I suppose. Have you the unit test failing ?

In your controller

def update
    @standing = Standing.find(params[:id])
    Standing.all_false_instead_of(@standing)
end

In your code you never push default_standing to true in you @standing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文