Tag: activerecord

将一个对象数组转换为ActiveRecord :: Relation

我有一个对象的数组,我们称之为Indicator 。 我想在这个数组上运行Indicator类的方法(那些def self.subjects变种,作用域等等)。 我知道在一组对象上运行类方法的唯一方法是让它们成为ActiveRecord :: Relation。 所以我最终诉诸添加to_indicators方法到Array 。 def to_indicators # TODO: Make this less terrible. Indicator.where id: self.pluck(:id) end 有时我链接了相当多的这些范围来筛选结果,在类方法中。 所以,即使我调用一个ActiveRecord :: Relation方法,我不知道如何访问该对象。 我只能通过all的内容来了解​​它的内容。 但是all都是一个数组。 那么我必须将该数组转换为ActiveRecord :: Relation。 例如,这是其中一种方法的一部分: all.to_indicators.applicable_for_bank(id).each do |indicator| total += indicator.residual_risk_for(id) indicator_count += 1 if indicator.completed_by?(id) end 我想这可以归结为两个问题。 如何将一个对象数组转换为一个ActiveRecord :: Relation? 最好不要每次都做一次。 在ActiveRecord :: Relation上运行def self.subjectstypes方法时,如何访问ActiveRecord :: Relation对象本身? 谢谢。 […]

Arel在Rails 3.0中究竟是什么?

我知道它是ActiveRecord的替代品,它使用对象而不是查询。 但… 为什么这更好? 将对象/查询“更容易”创build? 会导致更有效的SQL查询? 它将与所有主要数据库兼容吗? – 我认为会的。 使用存储过程会更容易吗?

LEFT OUTERjoinRails 3

我有以下代码: @posts = Post.joins(:user).joins(:blog).select 这意味着find所有的职位,并返回他们和相关的用户和博客。 但是,用户是可选的,这意味着INNER JOIN :joins生成不会返回大量的logging。 我如何使用它来生成一个LEFT OUTER JOIN呢?

如何在Rails中获得一个属性的原始值

有没有办法获得一个ActiveRecord属性的原始值(=从数据库中加载的值)? 我想要一个观察者这样的东西 before_save object do_something_with object.original_name end 任务是在更新时从哈希表中删除对象(实际上,将其移动到表中的另一个键)。

Rails – validation协会的存在?

我有一个与另一个模型B有一个“has_many”关联的模型A.我有一个业务需求,插入到A需要至less有一个关联的loggingB.是否有一个方法,我可以调用,以确保这是真实的,还是我需要写一个自定义validation?

Rails 4 LIKE查询 – ActiveRecord添加引号

我正在尝试像这样做一个类似的查询 def self.search(search, page = 1 ) paginate :per_page => 5, :page => page, :conditions => ["name LIKE '%?%' OR postal_code like '%?%'", search, search], order => 'name' end 但是当它运行的时候,就是添加引起sql语句出现的引号 SELECT COUNT(*) FROM "schools" WHERE (name LIKE '%'havard'%' OR postal_code like '%'havard'%')): 所以你可以看到我的问题。 我使用的Rails 4和Postgres 9这两个我从来没有用过,所以不知道如果它和一个activerecord的事情或可能postgres的事情。 我如何设置这个,所以我喜欢在结束查询中的'%my_search%' ?

validation导轨的自定义消息3

Rails已经引入了新的方法来validation模型中的属性。 当我使用 validates :title, :presence => true 它工作,但是当我尝试添加自定义消息 validates :title, :presence => true,:message => "Story title is required" 产生错误 Unknown validator: 'message'

如何实现has_many:通过与Mongoid和mongodb的关系?

使用Rails指南中的这个修改过的示例,如何使用mongoidbuild模关系“has_many:through”关联? 挑战是,mongoid不支持has_many:通过ActiveRecord。 # doctor checking out patient class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments has_many :meeting_notes, :through => :appointments end # notes taken during the appointment class MeetingNote < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments has_many :physicians, :through => :appointments end # the patient class Patient < ActiveRecord::Base has_many […]

如何在使用活动logging时列出为数据库定义的所有表?

在使用活动logging时,如何获取为数据库定义的所有表的列表?

Rails 4中使用has_many:through:uniq时的弃用警告

Rails 4在使用:uniq => true和has_many:through时引入了弃用警告。 例如: has_many :donors, :through => :donations, :uniq => true 产生以下警告: DEPRECATION WARNING: The following options in your Goal.has_many :donors declaration are deprecated: :uniq. Please use a scope block instead. For example, the following: has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment' should be rewritten as the following: has_many :spam_comments, -> […]