草庐IT

variation_model

全部标签

ruby-on-rails - rails : Order a model by the last element of a has_many association

我有两个模型:User和Message,它们通过has_many关系连接。我想获得一个users列表,按他们上一条消息的时间戳排序。classUser当我这样做时:@users=User.includes(:messages).order('messages.created_atDESC').limit(5)它似乎对消息进行排序,获取最新的5条消息,然后返回与这些消息关联的用户。所以用户数可以少于5个。我想确保获得5个用户。我希望查询为每个请求获取最新消息,对最后的消息进行排序,然后将最新消息返回给用户。所以我想要这样的东西:@users=User.includes(:messages

ruby-on-rails - 目录 : Model validation question

我有一个基本的ActiveRecord模型,其中我有两个要验证的字段。要求是至少有一个字段必须有值。两者都可以有值,但至少有一个需要有值。我该如何表达validates_presence_of声明?例如:validates_presence_of:main_filevalidates_presence_of:alt_file我不希望只有其中一个为空时才产生错误,只有两个都为空时才产生错误。 最佳答案 validates_presence_of:main_file,:if=>Proc.new{|p|p.alt_file.blank?}

ruby-on-rails - Rails 教程 3 第 7 章 : User Model Rspec Test Failing

我一直在成功地遵循Rails教程3,直到我到达第7章并实现了用户模型,现在我的rspec一直失败。这是我的user.rb文件输出classUsertrue,:length=>{:maximum=>50}validates:email,:presence=>true,:format=>{:with=>email_regex},:uniqueness=>{:case_sensitive=>false}validates:password,:presence=>true,:confirmation=>true,:length=>{:within=>6..40}before_save:encr

ruby-on-rails - rails : force a model to be not valid

我有一个非常特殊的情况,我想强制模型实例无效。像这样:user=User.newuser.valid?#trueuser.make_not_valid!user.valid?#false有什么办法可以实现吗?谢谢! 最佳答案 你可以这样做:validate:forced_to_be_invaliddefmake_not_valid!@not_valid=trueendprivatedefforced_to_be_invaliderrors.add(:base,'hasbeenforcedtobeinvalid')if@not_val

ruby-on-rails - NilClass :Class 的未定义方法 'model_name'

浏览器在Locals#show中返回以下错误:NilClass:Class的未定义方法'model_name'51:52:53:54:这是我的locals_controller.rb(显示Action)defshow@local=Local.find(params[:id])@foods=@local.foods.paginate(page:params[:page])respond_todo|format|format.html#show.html.erbformat.json{renderjson:@local}end结束这里是foods_controller.rb(创建操作)de

ruby-on-rails - ruby rails : Access information from other models?

另一个RubyonRails新手问题:在我的帖subview中,我想显示作者姓名。post数据库表存储作者id,它与users表中的usersid列相同。该用户在用户表中也有一个用户名列。有了用户id,我该如何获取用户名呢? 最佳答案 User.find(user_id).username是一种方式。如果你有一个belongs_to帖子和用户之间的关系是post.user.username这更像是“Rails方式”。 关于ruby-on-rails-rubyrails:Accessinf

ruby-on-rails - rails : Undefined method `truncate' in model

我的模型中有以下方法可以裁剪记录的描述,但由于未知原因截断方法不起作用:defcropped_descriptionnb_words_max=500ifdescription.length>nb_words_maxtruncate(description,:length=>nb_words_max,:separator=>'')+"..."elsedescriptionendend有人看到我做错了什么吗?谢谢。 最佳答案 你用错了,你应该在String上调用这个方法。参见truncate的签名。使用:ifdescription.le

ruby-on-rails - 从 has_many :through join models in Rails 中优雅地选择属性

我想知道从has_many中的连接模型中选择属性的最简单/最优雅的方法是什么:通过关联。假设我们有具有以下Item类的Items、Catalogs和CatalogItems:classItem:catalog_itemsend此外,假设CatalogueItems有一个位置属性,并且在任何目录和任何项目之间只有一个CatalogueItem。检索位置属性的最明显但有点令人沮丧的方法是:@item=Item.find(4)@catalog=@item.catalogs.first@cat_item=@item.catalog_items.first(:conditions=>{:cata

ruby-on-rails - 炼油厂 CMS : generating migrations for existing models within an engine

我想向我在RefineryCMS引擎中创建的模型添加一个属性。我知道我可以执行以下操作:rails生成迁移AddPartNumberToProductspart_number:string手动将迁移文件从db/migrate移动到vendor/extensions/products/db/migrate但是是否有命令首先将迁移生成到正确的文件夹中?谢谢! 最佳答案 根据@parndt(theleaddeveloperoftheRefineryproject),我上面概述的方法是正确的。

ruby-on-rails - RoR - Model.all,但没有 created_at 和 updated_at 字段

这个问题在这里已经有了答案:Rails4-Remove"created_at"and"updated_at"fromrender(2个答案)关闭5年前。我是Ruby的新手,所以这个问题对你来说可能看起来很愚蠢,是的。但还是...我有一个模型组,当我运行这个classGroupsController...然后我收到这样的数组响应:[{"id":1020,"name":"НІ-16-1","has_subgroups":true,"created_at":"2017-09-29T10:14:08.000Z","updated_at":"2017-09-29T10:14:08.000Z"}.