arel

Is there a way to invert an ActiveRecord::Relation query?

放肆的年华 提交于 2019-12-03 06:00:17
Let's say we have the following: irb> Post.where(:hidden => true).to_sql => "SELECT `posts`.* FROM `posts` WHERE posts.hidden = 1" Could we somehow get an inverted SQL query out of it? What I am looking for, should probably look like this: irb> Post.where(:hidden => true).invert.to_sql => "SELECT `posts`.* FROM `posts` WHERE NOT (posts.hidden = 1)" With a different syntax, yes. Example: posts = Post.scoped.table # or Arel::Table.new("posts") posts.where(posts[:hidden].eq(true).not).to_sql # => SELECT FROM `posts` WHERE NOT ((`posts`.`hidden` = 1)) In rails 4 there is not suffix for this

Ruby / Rails - Can I use a joined table's scope(or class method) as part of my WHERE clause?

狂风中的少年 提交于 2019-12-03 05:50:50
I want to grab all the categories that contain purchaseable products . class Product < ActiveRecord::Base belongs_to :category scope :purchaseable, where(:available => true) end class Category < ActiveRecord::Base has_many :products scope :with_purchaseable_products, ????? end So, I'm trying to define :with_purchaseable_products . This works: scope :with_purchaseable_products, joins(:products).where("products.available is true").group(:id).having('count(products.id) > 0') But that's not very DRY. Is there any way to apply my :purchaseable scope to products in my :with_purchaseable_products

Ransack: How to use existing scope?

喜你入骨 提交于 2019-12-03 05:31:42
Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic . Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: class Post < ActiveRecord::Base scope :year, lambda { |year| where("posts.date BETWEEN '#{year}-01-01' AND '#{year}-12-31'") } end So far as I know, this can be achieved by defining a custom ransacker . Sadly, I don't find any documentation about this. I tried this in the Post class: ransacker :year, :formatter => proc {|v| year(v) } But this does not work: Post.ransack(:year_eq => 2012).result.to

How to override :order defined in a has_many

痴心易碎 提交于 2019-12-03 05:30:36
问题 I have class Authors has_many :books, :order => 'name ASC' I am trying to query all the books sorted by name DESC Authors.books.order('name DESC') but the result is SELECT * FROM .... ORDER BY name ASC, name DESC and the results come back with the name sorted ASC is there a way to remove the original order in the association or override it? Or is specifying an order in a relation a bad idea? using Rails 3.0.3 回答1: Use reorder: Authors.books.reorder('name DESC') 回答2: .reorder() has been

How to join a table and count records in Rails 3?

谁说胖子不能爱 提交于 2019-12-03 05:26:31
I have a Collection class which has many coins. I am trying to select collections which have more than two coins. Currently, I have no problem doing that through straight Ruby, but that's extremely inefficient. My current code: collections = Collection.all.select { |c| c.coins.count > 2 } How do I achieve that through a joins call with Arel? Thanks! Yuval Karmi To answer my own question: Collection.joins(:coins).group("coins.collection_id").having("count(coins.id) > 2") Hat tip to KJF who asked this similar question and to krakover for answering it. Add counter_cache columns and query them.

Rails 3 has_many :through + join table conditions / scoping

对着背影说爱祢 提交于 2019-12-03 05:17:45
问题 I'm working on an app that has the models User and Project , and User can be assigned to multiple Project s, via ProjectUser , with a role (e.g. Developer, Designer). Project has_many :project_users has_many :users, :through => :project_users User has_many :project_users has_many :projects, :through => :project_users ProjectUser (user_id, project_id, role) belongs_to :user belongs_to :project I can call @project.users and @user.projects , but since there are varying roles, I'd like to be a

Rails Arel selecting distinct columns

假如想象 提交于 2019-12-03 05:12:01
问题 I've hit a slight block with the new scope methods (Arel 0.4.0, Rails 3.0.0.rc) Basically I have: A topics model, which has_many :comments , and a comments model (with a topic_id column) which belongs_to :topics . I'm trying to fetch a collection of "Hot Topics", i.e. the topics that were most recently commented on. Current code is as follows: # models/comment.rb scope :recent, order("comments.created_at DESC") # models/topic.rb scope :hot, joins(:comments) & Comment.recent & limit(5) If I

How to find records that have duplicate data using Active Record

∥☆過路亽.° 提交于 2019-12-03 03:10:14
问题 What is the best way to find records with duplicate values in a column using ruby and the new Activerecord? 回答1: Translating @TuteC into ActiveRecord: sql = 'SELECT id, COUNT(id) as quantity FROM types GROUP BY name HAVING quantity > 1' #=> Type.select("id, count(id) as quantity") .group(:name) .having("quantity > 1") 回答2: Here's how I solved it with the AREL helpers, and no custom SQL: Person.select("COUNT(last_name) as total, last_name") .group(:last_name) .having("COUNT(last_name) > 1")

How to join on subqueries using ARel?

梦想与她 提交于 2019-12-03 00:23:54
I have a few massive SQL request involving join across various models in my rails application. A single request can involve 6 to 10 tables. To run the request faster I want to use sub-queries in the joins (that way I can filter these tables before the join and reduce the columns to the ones I need). I'm trying to achieve this using ARel. I thought I found the solution to my problem there: How to do joins on subqueries in AREL within Rails , but things must have changed because I get undefined method '[]' for Arel::SelectManager . Does anybody have any idea how to achieve this (without using

Rails Arel selecting distinct columns

£可爱£侵袭症+ 提交于 2019-12-02 18:31:20
I've hit a slight block with the new scope methods (Arel 0.4.0, Rails 3.0.0.rc) Basically I have: A topics model, which has_many :comments , and a comments model (with a topic_id column) which belongs_to :topics . I'm trying to fetch a collection of "Hot Topics", i.e. the topics that were most recently commented on. Current code is as follows: # models/comment.rb scope :recent, order("comments.created_at DESC") # models/topic.rb scope :hot, joins(:comments) & Comment.recent & limit(5) If I execute Topic.hot.to_sql , the following query is fired: SELECT "topics".* FROM "topics" INNER JOIN