arel

How to join a table and count records in Rails 3?

[亡魂溺海] 提交于 2019-12-20 19:16:12
问题 I have a Collection class which has many coins. I am trying to select collections which have more than two coins. Currently, I have no problem doing that through straight Ruby, but that's extremely inefficient. My current code: collections = Collection.all.select { |c| c.coins.count > 2 } How do I achieve that through a joins call with Arel? Thanks! 回答1: To answer my own question: Collection.joins(:coins).group("coins.collection_id").having("count(coins.id) > 2") Hat tip to KJF who asked this

ARel mimic includes with find_by_sql

梦想与她 提交于 2019-12-20 10:39:16
问题 I've got a fairly complex sql query that I'm pretty sure I can't accomplish with ARel (Rails 3.0.10) Check out the link, but it has a few joins and a where exists clause, and that I'm pretty sure is too complex for ARel. My problem however is that, before this query was so complex, with ARel I could use includes to add other models that I needed to avoid n+1 issues. Now that I'm using find_by_sql, includes don't work. I still want to be able to fetch these records and attach them to my model

How do you scope ActiveRecord associations in Rails 3?

风格不统一 提交于 2019-12-20 08:01:34
问题 I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many"). I have records which have permission columns. I would like to build a default_scope that takes my permission columns into consideration so that records (even those accessed through a relationship) are filtered. Presently, in Rails 3, default_scope (including patches I've found) don't provide a

How do I use functions like CONCAT(), etc. in ARel?

孤人 提交于 2019-12-18 16:32:32
问题 Is there a way to have ARel write (sanitized, possibly aliased, etc.) column names into CONCAT() and other SQL functions? Here's how to do it with AVG()... ?> name = Arel::Attribute.new(Arel::Table.new(:countries), :name) => #<struct Arel::Attributes::Attribute [...] ?> population = Arel::Attribute.new(Arel::Table.new(:countries), :population) => #<struct Arel::Attributes::Attribute [...] ?> Country.select([name, population.average]).to_sql => "SELECT `countries`.`name`, AVG(`countries`.

Rails 3: Arel for NOT EXISTS?

允我心安 提交于 2019-12-18 15:35:50
问题 How do you write a NOT EXISTS in Arel? I'm having trouble translating this query into Arel: SELECT * FROM deals WHERE NOT EXISTS ( SELECT 1 FROM reward_deals WHERE reward_deals.deal_id = deal.id AND NOT ( reward_deals.awarding_type = 'deal' AND reward_deals.deal_id = reward_deals.awarding_id ) ) 回答1: Here is the answer, with strange names because I don't know how to give names for a domain that is for me unknown. deals = Deal.arel_table reward_deals = RewardDeal.arel_table awarding_condition=

Rails 3: Arel for NOT EXISTS?

橙三吉。 提交于 2019-12-18 15:35:47
问题 How do you write a NOT EXISTS in Arel? I'm having trouble translating this query into Arel: SELECT * FROM deals WHERE NOT EXISTS ( SELECT 1 FROM reward_deals WHERE reward_deals.deal_id = deal.id AND NOT ( reward_deals.awarding_type = 'deal' AND reward_deals.deal_id = reward_deals.awarding_id ) ) 回答1: Here is the answer, with strange names because I don't know how to give names for a domain that is for me unknown. deals = Deal.arel_table reward_deals = RewardDeal.arel_table awarding_condition=

How to do “where exists” in Arel

♀尐吖头ヾ 提交于 2019-12-18 11:53:59
问题 How do you do a query that includes a "where exists" in Arel? For example on a query like this to show all the suppliers with at least one order: SELECT * FROM suppliers WHERE EXISTS (SELECT * FROM orders WHERE suppliers.supplier_id = orders.supplier_id); I see "exists" in the Arel docs http://rubydoc.info/gems/arel/2.0.7/Arel/Nodes/Exists but I'm having trouble using it. 回答1: Here you go: suppliers= Supplier.arel_table orders= Order.arel_table suppliers_with_orders = Supplier.where( Order

Issue when retrieving records with empty array

一曲冷凌霜 提交于 2019-12-17 11:22:27
问题 I have a table of around 100 Users and I also have an array of user ids. What I wanted to do is show all users who are not a part of this array of user ids. When I do something like this User.where('id NOT IN (?)', [9, 2, 3, 4]) It successfully returns the records where the user's id does not belong in that array. However if that array is empty like so User.where('id NOT IN (?)', []) It does not return any users back and the SQL query looks like this SELECT "users".* FROM "users" WHERE (id

How to do a LIKE query in Arel and Rails?

会有一股神秘感。 提交于 2019-12-17 02:40:09
问题 I want to do something like: SELECT * FROM USER WHERE NAME LIKE '%Smith%'; My attempt in Arel: # params[:query] = 'Smith' User.where("name like '%?%'", params[:query]).to_sql However, this becomes: SELECT * FROM USER WHERE NAME LIKE '%'Smith'%'; Arel wraps the query string 'Smith' correctly, but because this is a LIKE statement it doesnt work. How does one do a LIKE query in Arel? P.S. Bonus--I am actually trying to scan two fields on the table, both name and description, to see if there are

Rails Arel unit test error

倾然丶 夕夏残阳落幕 提交于 2019-12-13 21:12:50
问题 I have the following error: ERROR["test_character_should_be_valid", CharacterTest, 0.214787] test_character_should_be_valid#CharacterTest (0.21s) NoMethodError: NoMethodError: undefined method `val' for "$1":Arel::Nodes::BindParam app/models/user.rb:11:in `block in <class:User>' test/models/character_test.rb:8:in `setup' From the following test: test/models/character_test.rb: require 'test_helper' class CharacterTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User",