Tag: arel

有没有办法反转ActiveRecord :: Relation查询?

假设我们有以下内容: irb> Post.where(:hidden => true).to_sql => “SELECT `posts`.* FROM `posts` WHERE posts.hidden = 1” 我们能以某种方式得到一个倒置的SQL查询吗? 我在寻找什么,应该是这样的: irb> Post.where(:hidden => true).invert.to_sql => “SELECT `posts`.* FROM `posts` WHERE NOT (posts.hidden = 1)”

Arel:如何用OR干净地加入多个条件?

在我的Rails应用程序中,我遍历一个数组以创建必须由OR连接的条件列表。 以下是我目前这样做的基本流程。 conditions = nil set.each do |value| condition = value.to_condition conditions = conditions ? conditions.or(condition) : condition end 显然,它并不漂亮,但我仍然不完全了解Arel的方式。 它是否提供了更好的OR加入一组动态生成条件的方法?

你会如何使用Squeel进行这个Rails子查询?

我想使用Squeel重构下面的查询。 我想这样做,以便我可以链接其中的运算符并重新使用查询的不同部分中的逻辑。 User.find_by_sql(“SELECT users.*, users.computed_metric, users.age_in_seconds, ( users.computed_metric / age_in_seconds) as compound_computed_metric from ( select users.*, (users.id *2 ) as computed_metric, (extract(epoch from now()) – extract(epoch from users.created_at) ) as age_in_seconds from users ) as users”) 查询必须全部在DB中运行,并且不应该是混合Ruby解决方案,因为它必须对数百万条记录进行排序和切片。 我已经设置了问题,因此它应该针对普通的user表运行,以便您可以使用它的替代方案。 对可接受答案的限制 查询应返回具有所有常规属性的User对象 每个用户对象还应包括extra_metric_we_care_about , age_in_seconds和compound_computed_metric 通过在多个地方打印出一个字符串,查询不应该复制任何逻辑 – 我想避免两次做同样的事情 [更新]查询应该在DB中都可以执行,以便在返回到Rails之前可以在DB中对可能包含数百万条记录的结果集进行排序和切片 [更新]该解决方案适用于Postgres数据库 我想要的解决方案类型示例 下面的解决方案不起作用,但它显示了我希望实现的优雅类型 class User < ActiveRecord::Base # […]

Ruby / Rails – 我可以使用连接表的范围(或类方法)作为WHERE子句的一部分吗?

我想抓住包含可购买products所有类别。 class Product true) end class Category < ActiveRecord::Base has_many :products scope :with_purchaseable_products, ????? end 所以,我正在尝试定义:with_purchaseable_products 。 这有效: scope :with_purchaseable_products, joins(:products).where(“products.available is true”).group(:id).having(‘count(products.id) > 0’) 但那不是很干。 有没有办法将我的:purchaseable范围应用于我的:with_purchaseable_products范围? 谢谢。

订购嵌套的has_many:通过关联计数

我正在尝试按照与特定组的用户关联的降序频率顺序对标签进行排序。 (ActiveRecord和Rails 3.2 – 如果有帮助我也安装了Squeel!) 用户有标签,组有用户。 因此,组通过用户具有标签。 class Group < ActiveRecord::Base has_many :users_groups # join table has_many :users, through: :users_groups has_many :tags, through: :users class User < ActiveRecord::Base has_many :users_groups # join table has_many :groups, through: :users_groups has_many :taggings # join table has_many :tags, through: taggings class Tag < ActiveRecord::Base has_many :taggings # join table […]

Rails 4 – 如何在活动记录查询中为别名()和联接()提供别名

如何为例如includes()提供别名? 以下是: 用户:活动记录模型 学生:活动记录模型,inheritance自用户(STI) 老师:主动记录模型,inheritance自用户(STI) 项目:主动记录模型 这里有一些例子: 第一个案例(更多STI协会) Project.all.includes(:students, :teachers).order(‘teachers_projects.name ASC’) # order on teachers Project.all.includes(:students, :teachers).order(‘users.name ASC’) # order on students Rails自动使用别名:teachers SQL中的:teachers 。 如何覆盖这个,以便我可以在SQL中使用别名teachers而不是teachers_projects ? :students获得别名users 。 这个例子失败了: Project.all.includes(:students, :teachers).order(‘teachers.name ASC’) Project.all.includes(:students, :teachers).order(‘students.name ASC’) Project.all.includes(:students, :teachers).order(‘students_projects.name ASC’) 第二个案例(一个STI协会) 如果我只使用:students方法includes() :students (没有:teachers ),Rails使用STI基类名称users名称别名(没有附加_projects ) :students : Project.all.includes(:students).order(‘users.name ASC’) # order on students 这个例子失败了: Project.all.includes(:students).order(‘students.name […]

Rails 4通过单个属性查询唯一

所以这不仅仅是一个问题,而是我想要做的事情。 我有三个对象,叫做Items 我想做一个只返回每个唯一“name”属性中的一个的查询。 类似于Item.select(‘distinct(name), items.*’) 这不起作用,它仍然返回所有三个项目。 如何形成此查询以便它只返回:

Rails Arel选择不同的列

我使用新的scope方法(Arel 0.4.0,Rails 3.0.0.rc)轻微阻止了 基本上我有: topics模型, has_many :comments和comments模型(带有topic_id列),其belongs_to :topics 。 我正在尝试获取“热门话题”的集合,即最近评论过的主题。 目前的代码如下: # models/comment.rb scope :recent, order(“comments.created_at DESC”) # models/topic.rb scope :hot, joins(:comments) & Comment.recent & limit(5) 如果我执行Topic.hot.to_sql ,则会触发以下查询: SELECT “topics”.* FROM “topics” INNER JOIN “comments” ON “comments”.”topic_id” = “topics”.”id” ORDER BY comments.created_at DESC LIMIT 5 这样可以正常工作,但它可能会返回重复的主题 – 如果主题#3最近被多次评论过,则会多次返回。 我的问题 我将如何回归一组不同的主题,记住我仍然需要访问comments.created_at字段,以显示上一篇文章的持续时间? 我会想象一下像distinct或group_by这样的东西,但我不太清楚如何最好地去做。 非常感谢任何建议/建议 – 我已经添加了100个代表奖金,希望很快能找到一个优雅的解决方案。

Rails 3:Arel for NOT EXISTS?

你如何在Arel写一个NOT EXISTS ? 我在将此查询转换为Arel时遇到问题: SELECT * FROM deals WHERE NOT EXISTS ( SELECT 1 FROM reward_deals WHERE reward_deals.deal_id = deal.id AND NOT ( reward_deals.awarding_type = ‘deal’ AND reward_deals.deal_id = reward_deals.awarding_id ) )

WHERE子句中的OR运算符与Rails 4.2中的Arel

以下代码在Rails 4.1中构造了一个带有OR运算符的有效where子句 MyModel.where( MyModel.where(attribute1: 1, attribute2: 2).where_values.reduce(:or) ) 这大致相当于SQL select * from my_models where (attribute1 = 1 OR attribute2 = 2) 在Rails 4.2中,相同的代码生成一个SQL查询,其缺少值的绑定参数 select * from my_models where attribute1 = OR attribute2 = …并且由于缺少绑定值的值而生成错误。 Rails 4.2中用OR运算符生成有效查询的等效代码是什么? 编辑: 该解决方案需要使用Arel::Nodes::Node派生对象,以便它本身可以通过AND和OR分组与其他条件组合。 rel = MyModel.where(attribute1: 1, attribute2: 2) conditions = [rel.where_values.reduce(:or).to_sql, *rel.bind_values.map(&:last)] MyModel.where(conditions) conditions var必须是Arel::Nodes::Node的衍生物。 上述解决方案适用于简单查询,但对于更复杂的查询, conditions必须是要传递给最终查询方法的Arel节点。