arel

Rails Active Record Query for Double Nested Joins with a Select Call

混江龙づ霸主 提交于 2019-12-11 00:30:09
问题 I have the following schema: Photos has many Groups has many Users. I am using this Rails server as a backend to an iOS application and constantly need to push out notifications to all involved users of a group when a photo is added. I problem is finding the least expensive query to resolve only the User.ids affected. So far I have Photo.find(1).groups.joins(:users) I know that I have to put a select argument after this, but can't figure out the syntax for the life of me. Given a photo, I am

Deleting VS Finding Orphans using ActiveRecord helpers

安稳与你 提交于 2019-12-10 19:34:25
问题 I'm trying to delete all the organizations that no longer have any users . Using the below code, I can find all the records I wish to delete: Organization.includes(:users) .where(users: { id: nil }) .references(:users) When I add delete_all , I get the same error I would get if I didn't include references : PG::UndefinedTable: ERROR: missing FROM-clause entry for table "users" I could probably write the solution in pure SQL, but I don't understand why Rails isn't keeping the reference to

Is there any way to use AREL for custom associations?

送分小仙女□ 提交于 2019-12-10 17:08:22
问题 model Post # ActiveRecord associations have tons of options that let # you do just about anything like: has_many :comments has_many :spam_comments, :conditions => ['spammy = ?', true] # In Rails 3, named scopes are ultra-elegant, and let you do things like: scope :with_comments, joins(:comments) end Is there any way to use AREL, or an otherwise leaner syntax, to define custom associations as elegantly as named scopes? update I've decided it's not a good idea to put that sort of detail into an

Rails/Arel - precedence when combining AREL predicates

淺唱寂寞╮ 提交于 2019-12-10 14:00:01
问题 A model has two attributes of interest, forename and town . I want to search for Richard (London) and Brian (Madrid). In long-hand, p=Person.scoped pred1=p.table[:forename].eq('Richard').and(p.table[:town].eq('London')) pred2=p.table[:forename].eq('Brian').and(p.table[:town].eq('Madrid')) pred3=pred1.or(pred2) I would expect this to wrap the predicates in parentheses to maintain the integrity of the query. But looking at pred3.to_sql gives an unexpected response: "(`person`.`forename` =

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

喜欢而已 提交于 2019-12-10 02:03:59
问题 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:

Arel, Joins and Rails Queries

你。 提交于 2019-12-10 00:55:21
问题 I've been stuck on a problem recently for a little while and found my way to Arel which looks like it should allow me to do OR's in my queries. As a starting point I needed to convert an existing Rails 3 query to Arel and that's where I've run into problems. The following scope and query works as I would expect it to. It gives me the requests associated with a particular user's ads. #in the Request class scope :responder, lambda { |user| joins(:ad).where(:ads => { :user_id => user }) }

Arel causing infinite loop on aggregation

ぐ巨炮叔叔 提交于 2019-12-09 17:17:41
问题 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

problem: activerecord (rails3), chaining scopes with includes

白昼怎懂夜的黑 提交于 2019-12-09 13:17:23
问题 In Rails3 there seems to be a problem when chaining two scopes (ActiveRelations) that each have a different include: Consider these two scopes, both of which work fine on their own: First scope: scope :global_only, lambda { |user| includes(:country) .where("countries.area_id <> ?", user.area) } Work.global_only(user) => (cut list of fields from SQL for legibility) SELECT * FROM "works" LEFT OUTER JOIN "countries" ON "countries"."id" = "works"."country_id" WHERE (countries.area_id <> 3) Now

How to make attribute setter send value through SQL function

∥☆過路亽.° 提交于 2019-12-09 06:07:32
问题 I'm trying to make an attribute setter in an ActiveRecord model wrap its value in the text2ltree() postgres function before rails generates its sql query. For example, post.path = "1.2.3" post.save Should generate something like UPDATE posts SET PATH=text2ltree('1.2.3') WHERE id = 123 # or whatever What's the best way of doing this? 回答1: EDIT: To achieve exactly what you are looking for above, you'd use this to override the default setter in your model file: def path=(value) self[:path] =

Rails 3.2 (ActiveRecord) association order with eager loading and ordering

孤街浪徒 提交于 2019-12-08 13:51:06
问题 I came upon this behavior in rails that I can't seem to find in the documentation. It appears that if you complicate an ActiveRecord query to the point that it lumps the underlying SQL into a "complicated" query (see below) it doesn't honor ordering specified on the associations. Here's my example model: class Article < ActiveRecord::Base belongs_to :user has_many :categories, :order => :name attr_accessible :title, :user_id, :user end If I perform a "simple" query, I can get the associated