草庐IT

custom-attribute

全部标签

ruby-on-rails - rails : creating a custom data type/creating a shorthand

我想知道如何创建自定义数据类型以在rake迁移文件中使用。示例:如果您要创建模型,则可以在迁移文件中添加列。它可能看起来像这样:defself.upcreate_table:productsdo|t|t.column:name,:stringt.timestampsendend我想知道如何创建这样的东西:t.column:name,:my_custom_data_type之所以这样创建,例如一个“货币”类型,无非是一个精度为8,小数位数为2的小数。由于我只使用MySQL,所以这个数据库的解决方案已经足够了。感谢您的反馈和意见! 最佳答案

ruby-on-rails - 切换到 Rails 4.1 时出现错误 - `method_missing':ActiveRecord::Base:Class (NoMethodError) 的未定义方法 `whitelist_attributes='

从Rails4.0切换到Rails4.1时出现此错误:activerecord-4.1.8/lib/active_record/dynamic_matchers.rb:26:in`method_missing':undefinedmethod`whitelist_attributes='forActiveRecord::Base:Class(NoMethodError)我没有在我的应用程序的任何地方使用attr_accessible或attr_protected所以我想知道为什么我有问题。当迁移到Rails4.0时,我已经安装了我的application.rb:配置/应用程序.rb:c

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

ruby-on-rails - 如何修复 Rails 中 pg_attribute 表的缓慢隐式查询

在我们的生产环境中,我们注意到Rails应用程序频繁出现峰值(大约每1小时一次)。深入挖掘,这是由于以下查询在单个HTTP请求中累计运行时间超过1.5秒(称为100倍)。SELECTa.attname,format_type(a.atttypid,a.atttypmod),pg_get_expr(d.adbin,d.adrelid),a.attnotnull,a.atttypid,a.atttypmodFROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.adrelidANDa.attnum=d.adnumWHEREa.attrelid=

ruby-on-rails - 在模型中使用 self.attribute 和 attribute 有什么区别?

在RubyonRails中,在模型中使用self.attribute和attribute有什么区别?在此示例中,假设my_attr是存储在数据库中的用户属性。classUser 最佳答案 您的示例的不同之处在于第一个有效,第二个无效。您的第二个版本没有做任何事情(至少没有任何意义)。编写my_attr=123不等同于self.my_attr=123。相反,它会创建一个名为my_attr的局部变量并将其设置为123,然后立即到达方法的末尾并丢弃my_attr。整个方法本质上是一个no-op,它不会以任何方式影响模型的my_attr值。

ruby-on-rails - 尝试更新数据库值时,rails update_attributes 返回 false

希望这里有人能给我指出正确的方向。我有一个ControllerUpdatedef运行“update_attributes”。目前它返回false,没有错误消息。我是Ruby的新手,但不是编码的新手,这让我困惑了好几天!我正在尝试使用下面指定的值更新用户模型和数据库。defupdate#getcurrentlyloggedinuser@user=current_user#updateuserparamsbasedoneditform...if@user.update_attributes(params[:user])redirect_toprofile_path,:notice=>"Su

ruby-on-rails - CanCan load_and_authorize_resource 触发 Forbidden Attributes

我有一个使用强参数的标准RESTfulController。classUsersController在我的config/initializers中,我有文件strong_parameters.rbActiveRecord::Base.send(:include,ActiveModel::ForbiddenAttributesProtection)当我向CanCan的load_and_authorize_resource添加一个简单的调用时,我得到了1)UsersControllerPOSTcreatewithinvalidparamsre-rendersthe'new'template

ruby-on-rails - Rails + 延迟作业 : Where is the correct place to store custom job classes?

我是delayed_job的新手,我开始编写自己的“自定义作业”。每个自定义作业基本上只是一个常规的ruby​​类,但我不确定这些自定义作业类通常存储在项目目录结构中的何处。我在考虑lib,但此时lib似乎是一个垃圾抽屉:/(不过也许没关系)谢谢!! 最佳答案 常见的方法是在app文件夹下创建jobs文件夹。但您唯一应该关心的是它们是您的文件,因此请以您觉得最适合自己的方式进行。 关于ruby-on-rails-Rails+延迟作业:Whereisthecorrectplacetosto

ruby-on-rails - accepts_nested_attributes_for的替代方法-也许是virtus

我对rails比较陌生,最终找到了正确的使用accepts_nested_attributes_for的方法。然而,网络上有一些严肃的资源说,使用accepts_nested_attributes_for通常是一种不好的做法(比如这个one)。要避免accepts_nested_attributes_for需要做哪些更改,以及将附加类文件放在哪个文件夹中(我想需要一个附加类)。我读到virtus适合这个。对吗?下面是一个仍然使用accepts_nested_attributes_for的非常基本的示例(查找完整的示例here):模型classPerson控制器classPeopleCo

Ruby:动态生成attribute_accessor

我正在尝试从散列(使用嵌套散列)生成attr_reader,以便它自动镜像instance_variable创建。这是我目前所拥有的:data={:@datetime=>'2011-11-23',:@duration=>'90',:@class=>{:@price=>'£7',:@level=>'all'}}classEvent#attr_reader:datetime,:duration,:class,:price,:leveldefinit(data,recursion)data.eachdo|name,value|ifvalue.is_a?Hashinit(value,recur