草庐IT

cm-attribute

全部标签

ruby - 如何防止 update_attributes!快捷回调?

我想在保存事件之前进行检查,这样我就可以在每次更新特定字段时发出警报。我尝试使用“changed”来检测它,但是update_attributes简化了数据库验证,所以我无法使用changed?例如,这会注意到更改:m=Player.newm.name="Tom"m.changed?=>true但这不是:m=Player.newm.update_attributes!(name:"John")m.changed?=>false我有什么不同的想法吗? 最佳答案 changed?返回true如果您的对象上有非持久性更改。在您调用之后,该

ruby-on-rails - [13] :Array 的未定义方法 `assign_attributes'

我的应用设置为当product.sold属性为1时表示商品已售出并且不会显示在商店View中。我正在尝试获取它,以便在客户checkout商品时更新product.sold属性。这是我的Controller中应该将product.sold属性从nil更新为1的代码行:classChargesController这是我的联想:OrderItembelongs_to:productOrderItembelongs_to:orderProducthas_many:order_itemsOrderhas_many:order_items这是我在尝试购买ID为13的产品时遇到的错误NoMetho

ruby-on-rails - rails : #inspect not displaying attribute

我将@foo定义为类实例属性,并使用after_initialize回调在创建/加载记录时设置此值:classBlog但是,当我检查一个Blog对象时,我没有看到@foo属性:>Blog.first.inspect=>"#"我需要做什么才能让inspect包含它?或者反过来,inspect如何确定要输出什么?谢谢。 最佳答案 ActiveRecord根据数据库表中的列确定在检查中显示哪些属性:definspectattributes_as_nice_string=self.class.column_names.collect{|na

ruby-on-rails - rake 数据库 :seed thorwing Can't mass-assign protected attributes

我正在学习ruby​​onrails。我正在创建一个用于存储用户信息的模型,当调用rakedb:seed时出现以下错误,知道我遗漏了什么吗?rakedb:seedrequire'digest'classUsertrue,:length=>{:within=>5..50},:presence=>truevalidates:password,:confirmation=>true,:length=>{:within=>4..20},:presence=>true,:if=>:password_required?has_one:profilehas_many:articles,:order=

ruby - Rails 3 - 反序列化只返回 ActiveRecord::AttributeMethods::Serialization::Attribute

我最近将一个旧的Rails2.3应用程序(Ruby1.8.7)升级到了Rails3.2/Ruby1.9.3。使用ActiveRecord序列化并尝试访问序列化属性时,我得到:ActiveRecord::AttributeMethods::Serialization::Attributereturned..unserializethenreturnstheactualvalue.真正奇怪的是,有一些模型具有正常行为。有人可以帮助我吗? 最佳答案 我现在知道这个问题是什么时候发生的,但我仍然不知道为什么:这个有效:User选项返回{}如

ruby-on-rails - before_save 如果 attribute.present?

before_save:date_started_sets_deadline,ifdate_started.present?如果:date_started==nil,我不希望此before_save运行。我已经尝试了上述行的各种版本,所以不确定是否必须更改它或方法本身。defdate_started_sets_deadlineifself.date_started>Date.tomorrowself.deadline=self.date_startedendend我试图避免错误NoMethodError(undefinedmethod'>'fornil:NilClass):app/mo

ruby-on-rails - 在模型中使用 accepts_nested_attributes_for 时出现 Rails 4.1 错误

此类分配代码在railsv4.0.5之前是可以的但它在rails4.1.x中出错有一个apidocumentation所以我想它没有被弃用。我不知道这是什么问题。Loadingdevelopmentenvironment(Rails4.1.1)2.1.1:001>classAssignmentbelongs_to:group2.1.1:003?>has_one:event2.1.1:004?>accepts_nested_attributes_for:event2.1.1:005?>endNameError:undefinedlocalvariableormethod`generate

ruby-on-rails - 具有 mongoid 的许多对象的 update_attributes

可能是这样的:@users=User.criteria.for_ids(params[:user_ids])@users.update_all(:suspend=>true)代替:foruin@usersu.suspend=trueu.update_attributesend 最佳答案 看看这个:#Updatingonerecord:Person.update(15,:user_name=>'Samuel',:group=>'expert')#Updatingmultiplerecords:people={1=>{"first_na

ruby Chef 指令 : include_attribute vs. include_attributes

我在chef属性文件中看到了这段代码。include_recipe"deployment"include_attribute"postgresql"include_attribute"redis"include_attributes"uaa"include_attributes"service_lifecycle"有什么不同?include_attribute与include_attribute*S*我找不到任何关于include_attribute*S*的文档 最佳答案 include_attribute属性用于对收敛时加载属性文

ruby-on-rails - rails : validation fails with before_create method on datetime attribute

ApiKey.create!抛出验证错误:expires_at不能为空。classApiKey有属性t.datetime:expires_at但是,如果删除了验证,则before_create方法会在创建时起作用。这是为什么?-此模式适用于其他属性,例如access_tokens(字符串)等 最佳答案 我会说因为before_create在验证之后运行,也许您想将before_create替换为before_validation注意:如果你像那样留下回调,它会在你运行valid?或save或任何时候设置到期日期触发验证的事件记录方法