Tag: activerecord

ActiveRecord对子集或记录的计算

我有一个带有points属性的Game模型,我想计算前20个得分的总和。 我可以采取积分并让ruby计算总和,如: Game.order(‘points desc’).limit(20).pluck(:points).sum 但我很好奇是否有一种直接的方法让AR产生一个SQL聚合计算来实现同样的目的。 以下天真的尝试不起作用: Game.sum(:points, order: ‘points desc’, limit: 20) SELECT SUM(`games`.`points`) FROM `games` 谢谢

Rails 4生成无效的列名称

我有一个相当简单的查询来返回多对多关系中的第一个记录,或者如果它不存在则创建一个。 UserCategorization.where(category_id: 3, user_id: 5).first_or_create 我的模型看起来像: class UserCategorization < ActiveRecord::Base belongs_to :user belongs_to :category self.primary_key = [:user_id, :category_id] end 但是它在SQL中生成无效的列名: SQLite3::SQLException: no such column: user_categorizations.[:user_id, :category_id]: SELECT “user_categorizations”.* FROM “user_categorizations” WHERE “user_categorizations”.”category_id” = 3 AND “user_categorizations”.”user_id” = 5 ORDER BY “user_categorizations”.”[:user_id, :category_id]” ASC LIMIT 1 如果我从模型中删除self.primary_key = [:user_id, :category_id] ,它可以正确检索记录但无法保存,因为它不知道在WHERE子句中使用什么: SQLite3::SQLException: no such column: user_categorizations.: […]

Rails meta_search gem:按关联模型的计数排序

我正在使用meta_search对表中的列进行排序。 我的一个表列是特定模型的相关记录的计数。 基本上是这样的: class Shop true).count end end class InventoryRecord < ActiveRecord::Base belongs_to :shop #has a "current" boolean on this which I want to filter by as well end 在我的Shop #index视图中,我有一个表格列出了每个商店的current_inventory_count。 无论如何使用meta_search按此计数订购商店? 我无法使用current_inventory_count方法,因为meta_search只能使用返回ActiveRecord :: Relation类型的自定义方法。 我可以考虑这样做的唯一方法是做一些自定义SQL,其中包括“虚拟”列中的计数,并按此列进行排序。 我不确定这是否可能。 有任何想法吗? 我正在使用Rails 3.0.3和最新的meta_search。

在Ruby on Rails中使用Plucked Values

给定一个名为Album的模型,我想使用我采用的值。 对于初学者,我想尝试打印值。 我怎样才能做到这一点? 这是我目前的代码: my_arr = Album.where(title: “Greatest Hits”, artist: “The Beatles”).pluck(:publisher, :year_published) puts my_arr[0][0].to_a 为了使事情变得更简单,我将如何检索任何值,以便将其置于正常变量中? 我正在使用Rails 4和SQLite3。

引用列名称

我有一个包含2个“不常见”列的表 – order like 。 该表在两个数据库上复制MySQL和PostgreSQL 。 我需要相同的应用程序连接到两个数据库并在两个上使用相同的查询: PageModel.where(“`like` >= ?”, params[:liked]) 这只适用于MySQL 。 如何让ActiveRecord引用列名? 就像是: PageModel.where(“%s >= ?” % quote_column_name(:like), params[:liked]) 我找到了一个现在没用的方法 – 它只返回列名而不引用它。 http://www.rubydoc.info/docs/rails/3.2.8/ActiveRecord/ConnectionAdapters/Quoting:quote_column_name 也许它只是一个占位符,还有另一种方法可以做到这一点?

Rails 3拒绝急于加载

我在Rails 3.0.7中工作。 我有一些多对多和一些一对多的关联,无法加载。 我的协会是: Person has_many :friends Person has_many :locations through=> :location_histories Location belongs_to :location_hour Location_hour has_many :locations 在我的控制器中,我有以下内容: @people = Person.includes([[:locations=>:location_hour],:friends]).where(“(location_histories.current = true) AND (people.name LIKE ? OR friends.first_name LIKE ? OR friends.last_name LIKE ? OR (friends.first_name LIKE ? AND friends.last_name LIKE ?))”).limit(10).all 然后在我看来,我有: locations.current是一个定义为: scope :current, lambda { where(“location_histories.current = ?”, true) } […]

错误:缺少表“self”的FROM子句条目(Rails 4 Active Record事务,postgresql 9.4))

在创建模型交易时,我使用after_create在DealPrize表上创建奖品。 Deal和DealPrize属于/ has_many关系:a Deal有许多Deal奖品,Dealprize属于Deal。 它的工作方式如下:在Deal中,我有一个列’prize-number’,我使用了after_create,以便amdin创建一个新的交易evetytime,应用程序获取此prize_number列,并创建此批量的奖品(插入尽可能多的行必要时)在DealPrize表中。 我无法在postgresql查询中使用我的Rails app moel变量。 这是一个有效的查询( 使用数字代替变量 )。 它将它写在我的数据库上。 model deals.rb CONNEXION = ActiveRecord::Base.connection def create_prizes Deal.transaction do self.120000.times do |i| CONNEXION.execute “INSERT INTO deal_prizes (deal_id, created_at, updated_at) values ( 62, ‘2009-01-23 20:21:13’, ‘2009-01-23 20:21:13’)” end end end 但是,当我按照应变量替换示例值时,我得到一个错误。 CONNEXION = ActiveRecord::Base.connection def create_prizes Deal.transaction do self.120000.times do |i| CONNEXION.execute “INSERT INTO […]

ConnectionNotEstablished之后在JRuby中有一个multithreading环境

我有Rails(4.2.6)应用程序,我使用Thread(s)进行Rufus :: Scheduler引起的后台数据处理。 我在开发环境中有工作应用程序,但是当我在nginx +乘客上将它移动到生产时,我得到exceptionActiveRecord :: ConnectionNotEstablished ,即使没有足够的负载。 从Threads执行的ActiveRecord :: Base后代的所有调用都包含在内 ActiveRecord::Base.connection_pool.with_connection{} 我之前从未使用multithreading环境,但在对问题进行一些调查后,我建议可能就是这个问题。 在JRuby下尝试它是否合理,哪种包含是优先的? 主机系统是Ubuntu 14.04 x86_64。

ActiveRecord Sum Max连接导致具有多个nil id的数组

与Rails activerecord相同的问题:sum,max和join 除了得到奇怪的结果。 class CustomResumeline < ActiveRecord::Base has_many :resumeline_words end class ResumelineWord < ActiveRecord::Base belongs_to :custom_resumeline end CustomResumeline.joins(:resumeline_words).where(resumeline_words: {name: ["time", "data"]}).select('sum(resumeline_words.weight) as nb_votes').group('resumeline_words.custom_resumeline_id').order('nb_votes ASC') 结果 CustomResumeline Load (0.8ms) SELECT sum(resumeline_words.weight) as nb_votes FROM “custom_resumelines” INNER JOIN “resumeline_words” ON “resumeline_words”.”custom_resumeline_id” = “custom_resumelines”.”id” WHERE “resumeline_words”.”name” IN (‘time’, ‘data’) GROUP BY resumeline_words.custom_resumeline_id ORDER BY nb_votes ASC => #<ActiveRecord::Relation […]

Rails 4,has_many通过关联 – 查找关联的对象

我想知道为什么Item.where(category: x)不适合我。 我希望这个语句能够返回分类在类别x下的所有项目。 请看下面我的协会: class Category :categorizations end class Item :categorizations end class Categorization < ActiveRecord::Base belongs_to :item belongs_to :category end Category.find(1).items返回该类别的所有项目。 请参阅下面的byebug控制台输出,它进一步说明了我的观点。 看起来生成的SQl正在寻找Item表中的类别列 – 当然不存在。 有任何想法吗? 1: class ItemsController 6: end 7: (byebug) Item.where(category: 1) Item Load (0.6ms) SELECT “items”.* FROM “items” WHERE “items”.”category” = 1 # (byebug) Category.find(1).items Category Load (0.7ms) SELECT “categories”.* […]