标签: update-attributes

Rails:update_attribute vs update_attributes

Object.update_attribute(:only_one_field, "Some Value")
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")
Run Code Online (Sandbox Code Playgroud)

这两个都将更新对象,而不必明确告知AR更新.

Rails API说:

for update_attribute

更新单个属性并保存记录,而无需通过正常的验证过程.这对现有记录上的布尔标志特别有用.混合验证模块时,Base中的常规update_attribute方法将替换为此默认值.

for update_attributes

更新传入的Hash中的所有属性并保存记录.如果对象无效,则保存将失败并返回false.

因此,如果我不想验证对象,我应该使用update_attribute.如果我在before_save上有这个更新怎么办,它会堆栈溢出吗?

我的问题是update_attribute是否也绕过了之前的保存或只是验证.

另外,将散列传递给update_attributes的正确语法是什么...查看我顶部的示例.

ruby-on-rails callback update-attributes

250
推荐指数
8
解决办法
27万
查看次数

将布尔值传递给update_attributes的问题

我有以下型号:

class GuestCatering < ActiveRecord::Base

  # Validation
  validates :name, :presence => true
  validates :order_number, :presence => true
  validates :orderable, :presence => true

end
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用以下代码更新现有的GuestCatering时:

guest_catering.update_attributes(:orderable => false)
Run Code Online (Sandbox Code Playgroud)

来宾餐饮变量是有效的GuestCatering对象.guest_catering对象在更新后有错误,如下所示:

<{[:orderable, ["can't be blank"]]=>nil}>
Run Code Online (Sandbox Code Playgroud)

但是当我通过时orderable => true,一切都很好,没有错误.

这里有什么问题,为什么我不能设置为虚假的?

ruby activerecord model ruby-on-rails update-attributes

30
推荐指数
2
解决办法
4838
查看次数

嵌套属性update_attributes使用insert而不是update

我有一个用户和嵌套的配置文件类,如下所示:

class User < ActiveRecord::Base
  has_one :profile
  attr_accessible :profile_attributes
  accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
  attr_accessible :name
end

user = User.find(1)
user.profile.id  # => 1
user.update_attributes(profile_attributes: {name: 'some name'})
user.profile.id  # => 2
Run Code Online (Sandbox Code Playgroud)

我不明白为什么rails会丢弃旧配置文件并创建一个新配置文件.

运用

user.profile.update_attributes({name: 'some name'})
Run Code Online (Sandbox Code Playgroud)

只是按预期更新当前配置文件.但在那种情况下,我没有利用accepts_nested_attributes_for

有谁知道为什么更新会这样发生?我不希望最终得到一个没有连接到任何用户的配置文件行数据库.

ruby-on-rails nested-attributes update-attributes

28
推荐指数
3
解决办法
2万
查看次数

无需密码即可更新`User`属性

现在,用户可以编辑一些属性,而无需输入密码,因为我的验证设置如下:

validates :password, :presence =>true, :confirmation => true, :length => { :within => 6..40 }, :on => :create
validates :password, :confirmation => true, :length => { :within => 6..40 }, :on => :update, :unless => lambda{ |user| user.password.blank? } 
Run Code Online (Sandbox Code Playgroud)

但是,在用户执行此操作后,将删除其密码 - update_attributes将其密码更新为"".这是我的更新定义:

def update

    if @user.update_attributes(params[:user])
        flash[:success] = "Edit Successful."
        redirect_to @user
    else
        @title = "Edit user"
        render 'edit'
    end
end
Run Code Online (Sandbox Code Playgroud)

我也尝试使用另一种使用update_attribute的定义:

def save_ff
    @user = User.find(params[:id])
    @user.update_attribute(:course1, params[:user][:course1] )
    @user.update_attribute(:course2, params[:user][:course2] )
    @user.update_attribute(:course3, params[:user][:course3] )
    @user.update_attribute(:course4, params[:user][:course4] )
    redirect_to …
Run Code Online (Sandbox Code Playgroud)

passwords validation ruby-on-rails update-attributes

26
推荐指数
5
解决办法
2万
查看次数

如何在不执行"before_save"的情况下"update_attributes"?

我有一个before_save在我Message这样定义模型:

   class Message < ActiveRecord::Base
     before_save lambda { foo(publisher); bar }
   end
Run Code Online (Sandbox Code Playgroud)

当我做:

   my_message.update_attributes(:created_at => ...)
Run Code Online (Sandbox Code Playgroud)

foo并被bar执行.

有时,我想更新消息的字段而不执行foobar.

例如,如何更新created_at字段(在数据库中)而不执行foobar

ruby-on-rails before-filter update-attributes ruby-on-rails-3 before-save

24
推荐指数
3
解决办法
3万
查看次数

Rails update_attributes方法是否是在数据库中更新模型的最佳选择?

def update
  @album = Album.find(params[:id])
  if @album.update_attributes(params[:album])
    redirect_to(:action=>'list')
  else
    render(:action=>'edit')
  end    
end
Run Code Online (Sandbox Code Playgroud)

我正在讨论的Rails 1.1.6教程建议使用update_attributes更新模型的方法,如上面列出的控制器的示例代码中所示.看看Rails文档,我想知道为什么这个update方法不是首选,特别是因为它的逻辑命名.

controller model ruby-on-rails updatemodel update-attributes

17
推荐指数
1
解决办法
3万
查看次数

rails update_attributes在尝试更新db值时返回false

希望有人能指出我正确的方向.

我有一个控制器Update def运行"update_attributes".目前它返回false,没有错误消息.我对Ruby很新,但不是编码,这让我难以忍受了好几天!我试图使用下面指定的值更新用户模型和数据库.

def update   
    #get currently logged in user
    @user = current_user
    #update user params based on edit form...
    if @user.update_attributes(params[:user])
        redirect_to profile_path, :notice  => "Successfully updated profile."
    else
        render :action => 'edit'
    end
end
Run Code Online (Sandbox Code Playgroud)

我的编辑def ....

def edit
    @user = current_user
end
Run Code Online (Sandbox Code Playgroud)

表单将以下内容发送到此更新方法:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ?
_method: put
authenticity_token: 9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  first_name: Amanda
  last_name: Ross
  is_owner: '1'
  email: example@googlemail.com
commit: Update
action: update
controller: users
id: '20'
Run Code Online (Sandbox Code Playgroud)

并且params [:user]返回:

{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"example@googlemail.com"}
Run Code Online (Sandbox Code Playgroud)

所有这些字段都在模型中的attr_accessible中,我可以毫无问题地创建用户.

这里是development.log输出(对不起它有点乱)....

Started …
Run Code Online (Sandbox Code Playgroud)

ruby controller ruby-on-rails update-attributes ruby-on-rails-3

17
推荐指数
2
解决办法
2万
查看次数

Rails 3 - 如何保存(取消)选中的复选框?

我有一个表单(form_tag)几个复选框,如下所示:

<%=check_box_tag 'model_name[column_name]', 1, (@data.model_name.column_name == 1 ? true : false)%>
Run Code Online (Sandbox Code Playgroud)

并更新它们像:

variable = ModelName.find(params[:id])             
variable.update_attributes(params[:model_name])
Run Code Online (Sandbox Code Playgroud)

这只是片刻,当我检查一些复选框 - 发送它们,它们将被保存.没关系.但是当我取消选中所有复选框 - 发送表单 - 所以什么也没发生,在DB表中不会在列中设置值0 ...

你可以给我任何提示,如何解决?

先感谢您

forms checkbox model update-attributes ruby-on-rails-3

11
推荐指数
1
解决办法
8383
查看次数

Rails:如何检查"update_attributes"是否会失败?

要检查是否buyer.save会失败,我使用buyer.valid?:

def create
  @buyer = Buyer.new(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)

我怎么能检查是否update_attributes会失败?

def update 
  @buyer = Buyer.find(params[:id])
  if <what should be here?>
    my_update_database_method
    @buyer.update_attributes(params[:buyer])
  else
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails update-attributes ruby-on-rails-3

10
推荐指数
1
解决办法
1万
查看次数

调用update_attributes后查找哪些字段已更新?

我想知道在update_attributes语句之后哪些字段已更新.我正在过滤可更新的参数并删除那些我不想更新的参数params[:model].现在,一些新的可更新参数可能具有与旧的相同的值,并且我想知道哪个参数被更新以及哪一个被跳过,因为相同的值.以下是一些代码:

UPDATABLE_PARAMS = ["param1", "param2", "param3", "param4"]
def update
  @dr = DR.find(params[:id])
  authorize! :update, @dr #devise stuff

  hnew = params[:dr]
  hnew.delete_if {|k, v| !UPDATABLE_PARAMS.include?(k.to_s) }

  if @dr.update_attributes(hnew)
    @dr.update_attribute(:last_updated_by, current_user.email)
    @dr.touch
  end

  render :update_result
end
Run Code Online (Sandbox Code Playgroud)

这是棘手的部分:

我想@dr用JSON 渲染对象(但已经设置了),除了标准字段之外,我还想添加一个包含该对象的嵌套对象updated_params.我可以送hnew@hnew,以我的观点,但如果我这样做,我会得到所有的加工参数,可以不只是是不同的人.

我怎样才能获得改变的参数?

ruby-on-rails update-attributes

10
推荐指数
1
解决办法
3966
查看次数