Tag: activerecord

计算月统计

我有一个捐款表,我正在尝试计算每个月的总金额。 没有任何捐款的几个月,我希望结果返回0。 这是我当前的查询: Donation.calculate(:sum, :amount, :conditions => { :created_at => (Time.now.prev_year.all_year) }, :order => “EXTRACT(month FROM created_at)”, :group => [“EXTRACT(month FROM created_at)”]) 返回: {7=>220392, 8=>334210, 9=>475188, 10=>323661, 11=>307689, 12=>439889} 任何想法如何抓住空虚的月份?

翻译activetraord集合以获取下拉列表

在模型中我有一些状态 STATES = [“in_progress”, “active”, “archived”] 在我的表格中,我想要一个下拉/选择 In Progress, Active, Archived for english 和 ชำระ , ขัน , ยื่น in Thai 处理这个问题的最佳方法是什么? 我想到的一个选择如下 def self.states @states = {} STATES.each do |s| @states[s] = I18n.t(s) end @states end 有没有更好的办法?

Rails 3 ActiveRecord交易

我有一个模型方法,我想从各种控制器调用。 它看起来像这样: def Post < ActiveRecord::Base def read! self.read_at = Time.now self.save self.thread.status = Status.find_by_name("read") self.thread.save end end 在我的控制器中,如果我打电话给@post.read! ,这会回滚任何错误吗?

Rails根据父模型属性过滤子模型的记录

以下是1对M型号: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, … end class Upload < ActiveRecord::Base belongs_to :foto_gossip end 现在我想要Uploads.all的条件:published_at NOT NULL相应的上传的父模型?

使用DataMapper而不是ActiveRecord

DataMapper的想法肯定比ActiveRecord好。 它有一个用于各种数据存储的API,包括RDBMS和NoSQL存储。 DataMapper比ActiveRecord更智能。 它具有“战略急切加载”。 此function单手消除“N + 1查询问题”。 此外,它允许延迟加载像Text属性这样的重字段。 DataMapper允许您通过提供嵌套的条件哈希来创建和搜索任何复杂的对象图。 ActiveRecods不适合用于生产中的关联。 include方法很丑陋且不可配置。 2010年10月,Josh Symonds 为此方法制作了一个补丁,允许将字段排除在急切加载之外。 但是这条路被忽略了,今天在rails3中我们有同样丑陋的包含方法。 即使在具有出色AREL的Rails3中,ActiveRecord也远不是最好的轨道。 你可能会说“哇,新的gem metawhere允许编写类似Article.where的好代码Article.where(:title.matches => ‘Hello%’, :created_at.gt => 3.days.ago) ”。 但等等…… DataMapper支持这个开箱即用! 也许而不是修改ActiveRecord看起来像DataMapper更好的是扩展和支持更好的orm? 有关DataMapper的更多详细信息,请访问http://datamapper.org/ 。 我认为,后来版本中的rails应该允许我们选择使用哪个orm,就像现在它允许数据库选择一样。 即使它只有一个选项“ActiveRecord”,人们也会搜索可用的替代品。 当我开始学习rails时,我认为只支持ActiveRecord。 后来我甚至没有尝试寻找别的东西。 为什么我要写这些东西? 我想,我们需要更加关注这个漂亮的ORM。 如果您是一些流行或不那么受欢迎的gem的开发者,请考虑添加对DataMapper的支持。 DataMapper社区应该从ActiveRecord或其他ORM编写一些迁移指南,并保持文档的最新状态,您可以帮助他们。 至于我,DataMapper社区需要更多人,你可能就是其中之一。 这个ORM的唯一缺点是缺少文档,你可以提供帮助。 那你觉得怎么样?

Rails:如何使用self-refere实现计数器缓存通过has_many实现多对多:通过

如何使用has_many :through的自引用多对多关系来滚动自己的计数器缓存? 我需要跟踪每篇文章的引用次数和参考数量 我大致使用了这个问题答案中的代码: class Publication :citations, :source => :reference has_many :references, :foreign_key => “reference_id”, :class_name => “Citation” has_many :refered_publications, :through => :references, :source => :publication end class Citation “Publication” end

如何在没有其他查询的情况下访问has_many:通过关联对象的相关联接模型?

这是我现在遇到过很多次的事情,我很想知道如何做我想要的或构建并向Rails提交补丁。 很多时候在我的应用程序中,我会有一些看起来像这样的模型: class User < ActiveRecord::Base has_many :memberships has_many :groups, through: :memberships end class Membership belongs_to :user belongs_to :group def foo # something that I want to know end end class Group has_many :memberships has_many :users, through: :memberships end 我希望能够做的是通过调用该关联来访问相关的成员资格,而无需进行其他查询。 例如,我想做这样的事情: @group = Group.first @group.users.each do |user| membership = user.membership # this would be the […]

如何救援exception中心和DRY?

我有一个例外,在约20个单独的地方产生。 它可以在每个地方轻松地以相同的方式获救,但这不是干燥和放弃治疗工作! 我想在中心位置拯救这个例外。 我怎么安排这个? 它顺便说一下ActiveRecord::RecordNonUniqueexception,……

是否可以使用`ORDER BY FIELD`语句对多列进行排序?

我正在使用Ruby on Rails 3.2.2,我想知道是否可以通过使用ORDER BY FIELD语句来订购多个列/字段(例如,在Model.order(“orders_count, created_at, name, …”)但是对于ORDER BY FIELD )。 如果是这样,我怎么能“按ORDER BY FIELDS ”? PS :如前面的答案中所述 ,为了订购与一栏相关的记录,我使用的是Model.order(“field(status, ‘blocked’, ‘unpublished’, ‘published’)”)语句。

列中包含#的栏目名称和已损坏的activemodel

我有一个带有(可怕的)列名的遗留表,如LYEAR#2,LYEAR#3 …… LYEAR#9 我为它设置了一个名为Glamas的模型 如果我有: accounts = Glamas.all account_mains = accounts.collect(&:ACCOUNT_MAIN) 它打破了这个错误: SyntaxError: compile error /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:273: syntax error, unexpected kUNDEF, expecting ‘)’ undef :LYEAR#9? ^ /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:274: syntax error, unexpected kEND, expecting $end from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:274:in `define_attribute_methods’ from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `each’ from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `define_attribute_methods’ from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `each’ from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `define_attribute_methods’ from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:13:in `define_attribute_methods’ from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:41:in `method_missing’ from (irb):31:in […]