Tag: 包括

Rails ActiveRecord:是否可以组合:include和:conditions查询?

想象一下,我有维基文章,有许多修订。 我想通过数据库对ActiveRecord进行查询,该数据库仅返回那些具有在过去24小时内更新的修订的文章。 这样的事情可能吗? 我想它会是这样的: Articles.find_all(:include => :revisions, :conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc}) 如果这是不可能的,是否有某种类型的原始SQL我可以传递给find_by_SQL,这样做的诀窍呢?

to_xml include在rails 3.0.6 ruby​​ 1.9.2中不起作用

我已将我的服务器更新为ruby 1.9.2,这已停止工作(rails 3.0.6): def index @musicians = Musician.includes(:instruments) render :xml => @musicians.to_xml( :include => :instruments ) end 和模型: class Musician < ActiveRecord::Base has_and_belongs_to_many :instruments end class Instrument < ActiveRecord::Base has_and_belongs_to_many :musicians end 我收到这个错误: undefined method `type’ for nil:NilClass 框架跟踪: activesupport (3.0.6) lib/active_support/whiny_nil.rb:48:in `method_missing’ activerecord (3.0.6) lib/active_record/serializers/xml_serializer.rb:230:in `compute_type’ activemodel (3.0.6) lib/active_model/serializers/xml.rb:22:in `initialize’ activemodel (3.0.6) lib/active_model/serializers/xml.rb:75:in `new’ […]

自动包含在Rails控制台中

我发现自己必须输入(例如) 包括PathHelper 每次我加载Rails控制台。 有没有办法配置Rails控制台自动包含某些模块?

Rails lib包括

关于lib目录中定义的模块,我有一个令人费解的问题 我有两个文件 #lib/authentication.rb module Authentication end #lib/test_module.rb module TestModule end 在我的应用程序控制器中我有 class ApplicationController < ActionController::Base include Authentication include TestModule end 身份validation模块正确加载但TestModule没有 我得到“未初始化的常量ApplicationController :: TestModule” 我很难过……有人吗? 编辑:有谁知道我可以在哪里调试这个?

Rails:为什么在我的模型中无法识别“number_with_delimiter”方法?

我有一个简单的validation: class Product 1000000, :message => “must be less than #{number_with_delimiter(1000000)}” … end 在此代码中,我收到以下错误: undefined method `number_with_delimiter’ for # 我试着添加: include ActionView::Helpers::NumberHelper 但它没有帮助。 我错过了什么?

Ruby on Rails ::包含与子模型的多态关联

使用多态关联时,是否可以在仅存在于某些类型中的子模型上运行包含? 例: class Container belongs_to :contents, :polymorphic => true end class Food has_one :container belongs_to :expiration end class Things has_one :container end 在视图中我将要做的事情如下: 因此,当我加载c时,我想急切加载到期,因为我知道我将需要最后一个。 有没有办法这样做? 只定义一个常规:include得到我的错误,因为并非所有封闭类型都有子模型到期。

在Object中的模块中混合会导致所有对象将该模块的实例方法作为单例方法inheritance

当我尝试将自己的行为添加到Object类时,我会得到在将模块混合到用户定义的类时不会发生的不良影响。 module Entity def some_instance_method puts ‘foo’ end def self.secret_class_method puts ‘secret’ end module ClassMethods def some_class_method puts ‘bar’ end end def self.included( other_mod ) other_mod.extend( ClassMethods ) end end 现在,我创建类Bob ,使其包含Entity 。 class Bob; include Entity; end; 这产生了期望和预期的输出: Bob.secret_class_method #=> NoMethodError Bob.some_instance_method #=> NoMethodError Bob.some_class_method #=> bar Bob.new.secret_class_method #=> NoMethodError Bob.new.some_instance_method #=> foo Bob.new.some_class_method […]

Will_paginate包括评论

我发现gemwill_paginate很棒! 但我面临使用问题。 我正在建立一个群组>发布>评论应用,所以在我的群组展示页面中,我正在显示post及其评论。 为了限制查询数量,我正在使用包含这样的方法: Group_controller: def show @posts = @group.posts.order(upd_at: :desc).includes(:user).includes(comments: :user).paginate(page: params[:page], per_page: 10) end 所以我想对我的评论进行分页。 你知道这样做的方法吗? 我的代码:Group_show = Groupe <div class="post_list”> 我的post/ _post = <ul id="comment_list”> 顺便说一句,如果你有一个方法直接在Groups_controller(show)中定义@comments,它可能真的很有用;)

Ruby包含模块中模块的单一方法

我有一个以下模块 module SimpleTask def task1 end def task2 end def task3 end end 我有一个模型只需要模块SimpleTask task2方法。 我知道在我的模型中include SimpleTask可以完成这项工作。 但我想知道我是否只能在我的模型中包含特定的task2方法。

Ruby Rake从gem中加载任务

我一直在尝试将位于github上的gem添加到我当前的应用程序中。 gem有一个rake文件,我希望能够从我的应用程序访问。 但我不断加载错误。 load ‘tasks/deploy.rake’ gem文件看起来像那样 # -*- encoding: utf-8 -*- require ‘rake’ Gem::Specification.new do |gem| gem.authors = %w(Hello World) gem.email = %w(test@example.com) gem.description = ‘test’ gem.summary = ‘test’ gem.homepage = ‘https://github.com/..’ gem.files = FileList[ ‘lib/**/*.rb’, ‘tasks/deploy.rake’, ‘README.md’ ].to_a gem.name = ‘test’ gem.require_paths = %w(lib) gem.version = ‘0.0.1’ end 我希望能够将./tasks/deploy.rake加载到包含此gem的应用程序中,我该如何继续呢? 谢谢