arel

ActiveRecord: can't use `pluck` after `where` clause with eager-loaded associations

巧了我就是萌 提交于 2019-12-05 01:29:13
I have an app that has a number of Post models, each of which belongs_to a User model. When these posts are published, a PublishedPost model is created that belongs_to the relevant Post model. I'm trying to build an ActiveRecord query to find published posts that match a user name, then get the ids of those published posts, but I'm getting an error when I try to use the pluck method after eager-loading my associations and searching them with the where method. Here's (part of) my controller: class PublishedPostsController < ApplicationController def index ar_query = PublishedPost.order(

How to do joins on subqueries in AREL within Rails

混江龙づ霸主 提交于 2019-12-04 18:28:24
问题 I have a simple model class User has_many :logs class Logs related in the usual way through the foreign key logs.user_id. I'm trying to do the following using Arel and according to the Arel doc it should work. u_t = Arel::Table::new :users l_t = Arel::Table::new :logs counts = l_t. group(l_t[:user_id]). project( l_t[:user_id].as("user_id"), l_t[:user_id].count.as("count_all") ) l_t.joins(counts).on(l_t[:id].eq(counts[:user_id])) When I do that I get the error TypeError: Cannot visit Arel:

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

拟墨画扇 提交于 2019-12-04 10:32:27
问题 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)" 回答1: With a different syntax, yes. Example: posts = Post.scoped.table # or Arel::Table.new("posts") posts.where(posts[:hidden].eq(true).not).to

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

筅森魡賤 提交于 2019-12-04 10:03:18
问题 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

How to join on subqueries using ARel?

回眸只為那壹抹淺笑 提交于 2019-12-04 08:37:13
问题 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

Rails 4 - how to give alias names to includes() and joins() in active record querying

喜欢而已 提交于 2019-12-04 08:23:09
问题 How can I give an alias name for e.g. includes() ? Following is given: User: active record model Student: active record model, inherits from User (STI) Teacher: active record model, inherits from User (STI) Project: active record model Here some examples: FIRST CASE (more STI associations) 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 uses

Arel: Left outer join using symbols

余生颓废 提交于 2019-12-04 08:18:09
I have this use case where I get the symbolized deep associations from a certain model, and I have to perform certain queries that involve using outer joins. How can one do it WITHOUT resorting to write the full SQL by hand? Answers I don't want: - using includes (doesn't solve deep associations very well ( .includes(:cars => [:windows, :engine => [:ignition]..... works unexpectedly ) and I don't want its side-effects) - writing the SQL myself (sorry, it's 2013, cross-db support, etc etc..., and the objects I fetch are read_only, more side-effects) I'd like to have an Arel solution. I know

Ordering nested has_many :through by count of associations

这一生的挚爱 提交于 2019-12-04 06:21:19
问题 I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!) Users have Tags, Groups have Users. Groups thus have Tags through Users. 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:

Arel causing infinite loop on aggregation

时光总嘲笑我的痴心妄想 提交于 2019-12-04 06:14:12
I have trouble with using Arel to aggregate 2 columns in the same query. When I run this, the whole server freezes for a minute, before the rails dev-server crashes. I suspect an infinite loop :). Maybe I have misunderstood the whole concept of Arel, and I would be grateful if anybody could have a look at it. The expected result of this query is something like this: [{:user_id => 1, :sum_account_charges => 300, :sum_paid_debts => 1000},...] a_account_charges = Table(:account_charges) a_paid_debts = Table(:paid_debts) a_participants = Table(:expense_accounts_users) account_charge_sum = a

How to properly add brackets to SQL queries with 'or' and 'and' clauses by using Arel?

北慕城南 提交于 2019-12-04 05:53:23
I am using Ruby on Rails 3.2.2 and I would like to generate the following SQL query: SELECT `articles`.* FROM `articles` WHERE (`articles`.`user_id` = 1 OR `articles`.`status` = 'published' OR (`articles`.`status` = 'temp' AND `articles`.`user_id` IN (10, 11, 12, <...>))) By using Arel this way Article .where( arel_table[:user_id].eq(1) .or(arel_table[:status].eq("published")) .or( arel_table[:status].eq("temp") .and( arel_table[:user_id].in(10, 11, 12, <...>) ) ) ) it generates the following ( note : brackets are not the same as the first SQL query): SELECT `articles`.* FROM `articles` WHERE