active-relation

How do I write a UNION chain with ActiveRelation?

不想你离开。 提交于 2019-12-03 11:34:11
问题 I need to be able to chain an arbitrary number of sub-selects with UNION using ActiveRelation. I'm a little confused by the ARel implementation of this, since it seems to assume UNION is a binary operation. However: ( select_statement_a ) UNION ( select_statement_b ) UNION ( select_statement_c ) is valid SQL. Is this possible without doing nasty string-substitution? 回答1: You can do a bit better than what Adam Lassek has proposed though he is on the right track. I've just solved a similar

Ruby on Rails - How to join two tables?

一曲冷凌霜 提交于 2019-12-03 10:08:11
I have two tables (subjects and pages) in one-to-many relations. I want to add criterias from subjects as well pages to parse a sql, but the progress has been very slow and often times running into problems. I'm brand new in rails, please help. class Subject < ActiveRecord::Base has_many :pages end class Page < ActiveRecord::Base belongs_to :subject end sample data in subjects, listed three columns below: id name level 1 'Math' 1 6 'Math' 2 ... Sample data in pages, listed columns below: id name subject_id -- -------------------- ---------- 2 Addition 1 4 Subtraction 1 5 Simple Multiplication

ActiveRecord Association select counts for included records

独自空忆成欢 提交于 2019-12-03 05:20:23
Example class User has_many :tickets end I want to create association which contains logic of count tickets of user and use it in includes (user has_one ticket_count) Users.includes(:tickets_count) I tried has_one :tickets_count, :select => "COUNT(*) as tickets_count,tickets.user_id " ,:class_name => 'Ticket', :group => "tickets.user_id", :readonly => true User.includes(:tickets_count) ArgumentError: Unknown key: group In this case association query in include should use count with group by ... How can I implement this using rails? Update I can't change table structure I want AR generate 1

multiple belongs_to relationships between two classes in Rails

淺唱寂寞╮ 提交于 2019-11-30 15:32:32
问题 I have a Transaction class. Each object of this class includes one issuing account, one sending account and one receiving account. Each of these is an instance of Account class. In my Transaction table, I have issuer_id, sender_id and receiver_id. How should I specify relationship between Transaction and Account so that I can call transaction.issuer transaction.sender transaction.receiver Thank you. 回答1: Use :class_name to specify the class name, when it can't be guessed from the association

multiple belongs_to relationships between two classes in Rails

天涯浪子 提交于 2019-11-30 14:38:29
I have a Transaction class. Each object of this class includes one issuing account, one sending account and one receiving account. Each of these is an instance of Account class. In my Transaction table, I have issuer_id, sender_id and receiver_id. How should I specify relationship between Transaction and Account so that I can call transaction.issuer transaction.sender transaction.receiver Thank you. meagar Use :class_name to specify the class name, when it can't be guessed from the association name: class Transaction belongs_to :issuer, :class_name => 'Account' belongs_to :sender, :class_name

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,

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 do I get Rails to eager load counts?

非 Y 不嫁゛ 提交于 2019-11-28 21:28:43
This is related to a question a year and change ago . I put up an example of the question that should work out of the box, provided you have sqlite3 available: https://github.com/cairo140/rails-eager-loading-counts-demo Installation instructions (for the main branch) git clone git://github.com/cairo140/rails-eager-loading-counts-demo.git cd rails-eager-loading-counts-demo rails s I have a fuller write-up in the repository, but my general question is this. How can I make Rails eager load counts in a way that minimizes db queries across the board? The n+1 problem emerges whenever you use #count

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 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 => {