activerecord-relation

Ambiguous table reference

筅森魡賤 提交于 2019-12-12 03:57:46
问题 This problem seems fairly simple, but I've never encountered one like this. Here are the settings: Post has_many :reader_links Post has_many :readers, :through => :reader_links I need to find out if there are readers reading a post. @post.reader_links.where('created_at >= ?', 45.minutes.ago).any? Works great. @post.readers.where('created_at >= ?', 45.minutes.ago),any? throws an ambiguous table column error because it's confused whether the created_at column means that of reader object or

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

Rails 4 Sum by Model Method

ε祈祈猫儿з 提交于 2019-12-04 16:23:55
问题 In my app, I have a User model, with a goal_ytd method, which performs some calculations. In a controller, I have a variable @users that might be User or an ActiveRecord::Relation of users , and I would like to sum all of the @users 's goal_ytd s. My first inclination was: @users.sum(&:goal_ytd) Which threw a deprecation warning in both cases because using sum on an ActiveRecord::Relation is going away in Rails 4.1. So, I changed the code to: @users.to_a.sum(&:goal_ytd) Which then threw a