Tag: update attributes

尝试在before_validation中设置变量,但它不起作用

因此,在我看来,我使用日历来选择日期和下拉以选择时间。 因此我使用before_validation方法将它放在一起: proposed_time.rb before_validation :construct_starting_at def construct_starting_at d = Time.parse(date) puts “************** construct_starting_at BEGIN *****************” puts “DATE: #{d}” puts “Time: #{time}” puts “Timezone: #{timezone}” puts “construct_starting_at :: #{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}” if date.present? && time.present? && timezone.present? starting_at = Time.zone.parse(“#{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}”) end puts “starting_at: #{starting_at}” puts “************** construct_starting_at END *****************” end 当我创建一个对象时它工作得很好,但是当我更新它时却没有。 日志 ************** construct_starting_at […]

Rails嵌套表单不更新

我在获取update_attributes以更新表单中的嵌套模型时遇到问题。 我没有任何错误,但嵌套属性不保存。 这是相关的代码: 用户型号: class User < ActiveRecord::Base has_many :orders has_many :achievements accepts_nested_attributes_for :achievements 成就模型: class Achievement < ActiveRecord::Base belongs_to :user 编辑用户表单: { :multipart => true } do |f| %> … 编辑方法: def edit @user = nil if params[:id] != nil @user = User.find(params[:id]) elsif @user = current_user else redirect_to login_path end 5.times { @user.achievements.build […]

update_attributes未保存到数据库

提交表单后,我的调试function中会显示正确的自定义参数,但是当我进入控制台时会显示默认的参数。 调节器 def update current_user.update_attributes(params[:user]) flash[:success] = “Your settings have been saved!” render new_status_update_path end 模型 attr_accessible :deficit_pct, :target_bf_pct, :activity_factor 笔记: 我在SO上可以找到的最接近的问题是一个改变通过关联存在的对象的属性的问题。 我尝试过使用Object.update方法,虽然我收到的错误是: 私有方法`update’调用# 有任何想法吗? 谢谢!

如何更新/重命名carrierwave上传的文件?

我无法弄清楚如何在rails 3.2.6中更新/重命名使用Carrierwave-mongoid上传/管理的文件。 我想重命名数据库中的文件以及文件系统。 这样的事可能…… def rename( id , new_name ) f = UploadedFile.find(id) if f.update_attributes({ f.file.original_filename: new_name }) # this is WRONG, what is right??? new_path = File.join( File.dirname( f.file.current_path ) , new_name )) FileUtils.mv( f.file.current_path , new_path ) end return f end 让我添加它是在它已经上传之后。

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

现在,用户可以编辑一些属性,而无需输入密码,因为我的validation设置如下: 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? } 但是,在用户执行此操作后,将删除其密码 – update_attributes将其密码更新为“”。 这是我的更新定义: def update if @user.update_attributes(params[:user]) flash[:success] = “Edit Successful.” redirect_to @user else @title […]

Carrierwave文件名在update_attributes上不断变化

我有模型公司和公司已经安装了carrierwave uploader Logo。 class Company < ActiveRecord::Base mount_uploader :logo, LogoUploader 图片上传工作,但我有update_attributes的问题。 当用户想要仅更新公司的描述或标题,而不是上传新图像时 – DB中的文件名值每次都在更改。 这是一个简单的例子: 1.9.3-p545 :004 > a = Company.last 1.9.3-p545 :005 > a.update_attributes(:title => “test title 2”) (0.4ms) BEGIN Company Exists (0.9ms) SELECT 1 AS one FROM `companies` WHERE (`companies`.`title` = BINARY ‘test title 2’ AND `companies`.`id` != 37) LIMIT 1 Company Load […]

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

我有一个用户和嵌套的配置文件类,如下所示: class User < ActiveRecord::Base has_one :profile attr_accessible :profile_attributes accepts_nested_attributes_for :profile end class Profile 1 user.update_attributes(profile_attributes: {name: ‘some name’}) user.profile.id # => 2 我不明白为什么rails会抛弃旧的配置文件并创建一个新的配置文件。 运用 user.profile.update_attributes({name: ‘some name’}) 只是按预期更新当前配置文件。 但在那种情况下,我没有利用accepts_nested_attributes_for 有谁知道为什么更新会这样发生? 我不希望最终得到一个没有连接到任何用户的配置文件行数据库。

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

我的Message模型中的before_save定义如下: class Message < ActiveRecord::Base before_save lambda { foo(publisher); bar } end 当我做: my_message.update_attributes(:created_at => …) foo和bar被执行。 有时,我想更新消息的字段而不执行foo和bar 。 如何在不执行foo和bar情况下更新created_at字段(在数据库中)?

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

Rails update_attribute

我有以下问题。 我有一个名为user的模型,它有一个名为activated的列。 我试图更新该方法激活方法?但它给我错误:validation失败:密码不能为空,密码太短(最少6个字符)这对我没有意义,因为我不接触密码字段! 我只想更新激活的列。 我把这些代码放在这里我认为它是相关的,但如果你认为你需要更多,请问:)非常感谢你提前! 模型: attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation, :activated has_many :sucu_votes email_regex = /\A[\w+\-.]+@[az\d\-.]+\.[az]+\z/i validates :name, :presence => true, :length => { :maximum => 50 } validates :email, :presence => true, :format => {:with => email_regex}, :uniqueness => { :case_sensitive => false } validates :password, :presence => true, :length => […]