arel

Rails/Arel: Selecting all records as an ActiveRecord::Relation

浪子不回头ぞ 提交于 2019-11-30 02:48:47
Using Arel in Rails - I'm looking for a way of creating an ActiveRecord::Relation that effectively results in SELECT * FROM table , which I can still manipulate further. For example, I have a model that's split up into multiple categories, and I return counts for these in the following manner: relation = Model.where(:archived => false) # all non-archived records record_counts = { :total => relation.count, :for_sale => relation.where(:for_sale => true).count :on_auction => relation.where(:on_auction => true).count } This works fine, and has the advantage of firing off COUNT queries to MySQL,

Where can I find good AREL documentation? [closed]

白昼怎懂夜的黑 提交于 2019-11-29 05:51:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm trying to learn as much as I can about AREL. But I'm not sure what to look at. I found some documentation on rubydoc, but it doesn't seem very good in terms of showing what are the "public API"/accessible things I can do. For example, I could not find any information on the "includes" method. So, how exactly

Rails/Arel: Selecting all records as an ActiveRecord::Relation

假装没事ソ 提交于 2019-11-29 00:32:05
问题 Using Arel in Rails - I'm looking for a way of creating an ActiveRecord::Relation that effectively results in SELECT * FROM table , which I can still manipulate further. For example, I have a model that's split up into multiple categories, and I return counts for these in the following manner: relation = Model.where(:archived => false) # all non-archived records record_counts = { :total => relation.count, :for_sale => relation.where(:for_sale => true).count :on_auction => relation.where(:on

How to exclude an array of ids from query in Rails (using ActiveRecord)?

社会主义新天地 提交于 2019-11-28 17:34:56
I would like to perform an ActiveRecord query that returns all records except those records that have certain ids. The ids I would like excluded are stored in an array. So: ids_to_exclude = [1,2,3] array_without_excluded_ids = Item. ??? I'm not sure how to complete the second line. Background: What I've already tried: I'm not sure background is necessary, but I've already tried various combinations of .find and .where. For example: array_without_excluded_ids = Item.find(:all, :conditions => { "id not IN (?)", ids_to_exclude }) array_without_excluded_ids = Item.where( "items.id not IN ?", ids

How to use unscoped on associated relations in Rails3?

被刻印的时光 ゝ 提交于 2019-11-28 16:37:41
I have a default scope on products due to information security constraints. class Product < ActiveRecord::Base has_many :photos default_scope where('visible = 1') end In my associated Photo model, however, I also have to find products that should not be visible. class Photo < ActiveRecord::Base belongs_to :product end my_photo.product In other cases, I can use unscoped in order to bypass the default_scope, e.g. in Product.unscoped.find_by_title('abc') . However: How to remove the scope when using associations of a record? my_photo.unscoped.product does not make sense as my_photo does not have

What exactly is Arel in Rails 3.0?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 15:04:55
I understand that it is a replacement for ActiveRecord and that it uses objects instead of queries. But... why is this better? will objects/queries be "easier" to create? will it lead to more efficient SQL queries? will it be compatible with all major DBs? - I assume it will. will it be easier/harder to use with stored procedures? Jörg W Mittag What exactly is Arel in Rails 3.0? It's an object model for an algebra of relational query operators. I understand that it is a replacement for ActiveRecord No, it isn't. It's a replacement for hand-crafting SQL queries in strings. It is a common query

ActiveRecord query with alias'd table names

时光毁灭记忆、已成空白 提交于 2019-11-28 09:24:59
Using model concerns which include scopes, what is the best way to write these knowing that nested and/or self-referencing queries are likely? In one of my concerns, I have scopes similar to these: scope :current, ->(as_at = Time.now) { current_and_expired(as_at).current_and_future(as_at) } scope :current_and_future, ->(as_at = Time.now) { where("#{upper_bound_column} IS NULL OR #{upper_bound_column} >= ?", as_at) } scope :current_and_expired, ->(as_at = Time.now) { where("#{lower_bound_column} IS NULL OR #{lower_bound_column} <= ?", as_at) } def self.lower_bound_column lower_bound_field end

Join the same table twice with conditions

我只是一个虾纸丫 提交于 2019-11-28 06:56:06
There are situations where ActiveRecord sets the alias table name if there are multiple joins with the same table. I'm stuck in a situation where these joins contain scopes (using 'merge'). I have a many-to-many relationship: Models table_name: users Second models table_name: posts Join table name: access_levels A Post has many users through access_levels and vice versa. Both, the User model and the Post model share the same relation: has_many :access_levels, -> { merge(AccessLevel.valid) } The scope inside of the AccessLevel model looks like this: # v1 scope :valid, -> { where("(valid_from IS

Ransack sort on count of HABTM or HMT associated records

自古美人都是妖i 提交于 2019-11-28 01:40:54
I have a HABTM relationship between the Theme and Quote models. The themes index view displays the count of quotes associated with each theme. I'd like to add a Ransack sort_link on that column, so the themes can be sorted by their count of associated quotes . I have done this successfully with has_many associations using a counter cache column, but Rails does not support counter cache columns for HABTM associations. So far, I've got a scope that adds a virtual attribute called quotes_count (by performing a single query, avoiding N+1) to the Theme model: scope :with_quotes_count, -> do joins(

How to exclude an array of ids from query in Rails (using ActiveRecord)?

痞子三分冷 提交于 2019-11-27 10:48:47
问题 I would like to perform an ActiveRecord query that returns all records except those records that have certain ids. The ids I would like excluded are stored in an array. So: ids_to_exclude = [1,2,3] array_without_excluded_ids = Item. ??? I'm not sure how to complete the second line. Background: What I've already tried: I'm not sure background is necessary, but I've already tried various combinations of .find and .where. For example: array_without_excluded_ids = Item.find(:all, :conditions => {