草庐IT

has_cpp_attribute

全部标签

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 : How to get has_many associations of a model

如何获取模型的has_many关联?例如,如果我有这个类:classA我想要这样的方法:A.get_has_many返回[B,C]这可能吗?谢谢! 最佳答案 您应该使用ActiveRecordreflections.然后你可以这样输入:A.reflect_on_all_associations.map{|assoc|assoc.name}这将返回你的数组[:B,:C] 关于ruby-on-rails-rails:Howtogethas_manyassociationsofamodel,我

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-on-rails - rails 4 : how to use named scope with has_many associations

在我的Rails4应用程序项目(模型)has_many视频(模型)中。我在视频模型中有一个命名范围:scope:live,where(is_deleted:0,sent_to_api:1)在我的一个项目View中,我这样做(项目是项目的一个实例):project.videos.live.size我希望得到的是那个特定项目中的项目数量,但我得到的是任何项目中的视频数量。就好像.live不是从.videos返回一个子集而是替换它。我看到它解释了here将命名范围相互链接起来应该与逻辑AND相结合,但是当应用于“关联方法”时[.videos在这种情况下的正确术语]似乎并不即将发生。正确的做法

ruby-on-rails - 如何跳过 has_secure_password 验证

在我的应用中,只有管理员可以创建新的用户记录。用户会通过电子邮件收到一个激活链接,他们可以在其中设置密码。我想使用has_secure_passord方法(railscast):classUser效果很好,但它会自动验证密码摘要的存在...所以当管理员创建记录时,验证失败。我有办法只跳过自动添加的password_digest验证而不跳过我添加的其他验证吗? 最佳答案 从4.X版本的rails开始,has_secure_password采用一个选项:validations。如果将其设置为false,它将不会运行验证。gem3.X版本

ruby-on-rails - has_many :through with counter_cache

据我了解,在定义:counter_cache选项时,应在包含belongs_to声明的模型上指定它。所以我有点不确定在通过关联使用has_may时如何处理这个问题(因为我相信在这种情况下不使用belongs_to声明):classPhysician:appointmentsendclassAppointmentappointment_countendclassPatient我希望使用:counter_cache选项来更有效地查找属于医生的患者数量。myPhysician.patients.count仅供引用:Rails3.1干杯 最佳答案

ruby-on-rails - has_many 同时尊重 factory_girl 中的构建策略

情况#ModelsclassUser:user)do|u|u.items{|items|[items.association(:item),items.association(:item)]}endFactory.define(:item)do|i|i.color"red"endFactory.define(:item_with_user,:parent=>:user)do|i|i.association(:user)end问题如果您运行@user=Factory(:user_with_items),则@user.items包含这两个项目。问题是这些项目未与数据库中的用户相关联。如果你

ruby-on-rails - 如何为 has_and_belongs_to_many 关系设置我的灯具?

我有以下模型:classCompany每当我收到新请求时,我想向同一地区的活跃公司发送通知。我如何在我的固定装置中设置它,以便我可以对寻找合适公司的逻辑进行单元测试?我试过了region_ids:1,2regions:one,two在companies.yml中,但都不能为公司分配区域。这里是生成的SQL的要点:https://gist.github.com/2713518 最佳答案 为了regions:one,two要在companies.yml中工作,您需要让Rails自动分配区域的ID。这是因为(为了避免必须在companie

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