{"id":5758,"date":"2014-04-06T14:07:07","date_gmt":"2014-04-06T14:07:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/problem-about-arel-collection-of-common-programming-errors\/"},"modified":"2014-04-06T14:07:07","modified_gmt":"2014-04-06T14:07:07","slug":"problem-about-arel-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/problem-about-arel-collection-of-common-programming-errors\/","title":{"rendered":"problem about arel-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5129fa69337d6006606c299cbee853d5?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncrispy<br \/>\nruby-on-rails activerecord arel scopes<br \/>\nI have encountered some unexpected behavior in Active Record (3.2.13):There is a simple scope on my model:class User &lt; ActiveRecord::Basescope :verified, lambda { where(&#8216;verified = 1&#8217;) } endThis can be used fine on its own:User.verified.to_sql#=&gt; &#8220;SELECT \\&#8221;users\\&#8221;.* FROM \\&#8221;users\\&#8221; WHERE (verified = 1)&#8221;When I concatenate where-clauses, they are anded as expected:User.where(company_id: 1).where(company_id: 2).to_sql&#8221;SELECT \\&#8221;users\\&#8221;.* FROM \\&#8221;users\\&#8221; WHERE \\&#8221;users\\&#8221;.\\&#8221;company_id\\&#8221; = 1 AND \\<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1b4a43f0c39b9b9c99f7c63a2f12ce53?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nfinpingvin<br \/>\nsql ruby-on-rails ruby ruby-on-rails-3 arel<br \/>\nI 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 =&gt; 1, :sum_account_charges =&gt; 300, :sum_paid_debts =&gt; 1000},&#8230;]a_account_charges = Table(:account_charges) a_paid_debts<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e7fac75b30a94ef87105e568ae4947eb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJack Juiceson<br \/>\nsql ruby-on-rails activerecord arel squeel<br \/>\ntl;drHow to convert below SQL to Arel(or whatever is considered standard in Rails)@toplist = ActiveRecord::Base.connection.execute(&#8216;select ci.crash_info_id,count(distinct(user_guid))[Occurences], c.md5 from crashes ci join crash_infos c on c.id=crash_info_id group by ci.crash_info_idorder by [Occurences] desc&#8217;)&#8212; end of tl;dr &#8212;-I&#8217;m working on a small web project, it&#8217;s goal is to take our customers crash reports(when our desktop app crashes, we send diagnostics to our servers), analyze them an<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/6c7b9836c14f4d86a6d9392df3f508bf?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nstephan.com<br \/>\nsql ruby-on-rails-3 arel<br \/>\nI&#8217;m desperately trying to make sense of Arel, mostly because I hate dealing with SQL; I was doing so well, but I&#8217;ve hit a wall.I&#8217;ve been working in Rails 3.0.0, and I&#8217;m trying to make a complex query with some math in it. The real case is rather more complex, but I&#8217;ve simplified a bit. In my example, I have a table with a particular string field, and I want a count of all the records, as well as a count for each of two possible values of that field, grouped by a foreign id.Under Rails 3.0.0, I<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/63e408bc29491c46e2e1425555ffe36c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nneezer<br \/>\nsql ruby-on-rails-3 postgresql activerecord arel<br \/>\nUPDATE: So thanks to @Erwin Brandstetter, I now have this:def self.unique_users_by_company(company)users = User.arel_tablecards = Card.arel_tableusers_columns = User.column_names.map { |col| users[col.to_sym] }cards_condition = cards[:company_id].eq(company.id).and(cards[:user_id].eq(users[:id]))User.joins(:cards).where(cards_condition).group(users_columns).order(&#8216;min(cards.created_at)&#8217;) end&#8230; which seems to do exactly what I want. There are two shortcomings that I would still like to have addr<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ed9686d6910478914966ec99bbcaa069?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAdam Lassek<br \/>\nruby-on-rails ruby-on-rails-3 join multiple-databases arel<br \/>\nI&#8217;m trying to move my User account and session data into a separate database so that we can eventually share it across multiple applications.I&#8217;ve seen plenty of people online saying to use establish_connection to tell a model to connect to a different db, but I am unable to get this to work.config\/database.ymldevelopment:adapter: mysql2encoding: utf8reconnect: truepool: 5host: localhostdatabase: project_name_developmentauthentication:adapter: mysql2encoding: utf8reconnect: truepool: 5host: local<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c78e40f8fc27a048f10ece6956a2fdf2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKevin Sylvestre<br \/>\nruby-on-rails ruby activerecord arel<br \/>\nI have the following example query:source = &#8220;(SELECT DISTINCT source.* FROM (SELECT * FROM items) AS source) AS items&#8221; items = Item.select(&#8220;items.*&#8221;).from(source).includes([:images]) p items # [#&lt;Item id: 1&gt;, #&lt;Item id:2&gt;]However running:p items.count Results in NoMethodError: undefined methodmap&#8217; for Arel::Nodes::SqlLiteral`I appreciate the query is silly, however the non-simplifieid query is a bit too complicated to copy and this was the smallest crashing version I could create. An<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/05e52599c776367d40161520b3c7dc88?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDavid Tuite<br \/>\nruby-on-rails ruby-on-rails-3 activerecord geocoding arel<br \/>\nI have a bus model and an event model. Each bus is going to an event:class Bus &lt; ActiveRecord::Basebelongs_to :eventbelongs_to :origin endclass Event &lt; ActiveRecord::Basehas_many :buses endAs you can see, each bus also has an origin (the place it leaves from). An origin can have many buses leaving from it. Origins are geocoded (by the [Geocoder gem][1]) on creation so that they have a latitude and longitude associated with them.class Origin &lt; ActiveRecord::Basehas_many :busesgeocoded_by<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/73ba2d202efab9804e1e344f2079f605?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDmytrii Nagirniak<br \/>\nsql ruby-on-rails ruby activerecord arel<br \/>\nHow can you combine 2 different conditions using logical OR instead of AND?NOTE: 2 conditions are generated as rails scopes and can&#8217;t be easily changed into something like where(&#8220;x or y&#8221;) directly.Simple example:admins = User.where(:kind =&gt; :admin) authors = User.where(:kind =&gt; :author)It&#8217;s easy to apply AND condition (which for this particular case is meaningless):(admins.merge authors).to_sql #=&gt; select &#8230; from &#8230; where kind = &#8216;admin&#8217; AND kind = &#8216;author&#8217;But how can you produce the f<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b74e5610efa082944d3200c13a5d92e9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nElliot<br \/>\nsql ruby-on-rails subquery arel<br \/>\nI am trying to build this query in ARel:SELECT FLOOR(AVG(num)) FROM ( SELECT COUNT(attendees.id) AS num, meetings.club_id FROM `meetings` INNER JOIN `attendees` ON `attendees`.`meeting_id` = `meetings`.`id` WHERE (`meetings`.club_id = 1) GROUP BY meetings.id) tmp GROUP BY tmp.club_idIt returns the average number of attendees per meeting, per club. (a club has many meetings and a meeting has many attendees)So far I have (declared in class Club &lt; ActiveRecord::Base):num_attendees = meetings.sel<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d678f8a8ee3bd76341e3aa31e2637769?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nnumber5<br \/>\nruby-on-rails-3 postgresql-9.1 arel sequel hstore<br \/>\nI would like to use the Squeel gem (based on Arel) for my Rails app (v 3.2.6). My hstore column is called properties.These work perfectly fine:User.where{(firstname == &#8216;Ryan&#8217;) &amp; (lastname == &#8216;Bates&#8217;)} User.where{&#8220;properties @&gt; (&#8216;male&#8217; =&gt; &#8216;1&#8217;)&#8221;}The second example is a plain Postgres query, because Squeel doesn&#8217;t seem to support hstore functions.These don&#8217;t work:User.where{&#8220;properties @&gt; (&#8216;male&#8217; =&gt; &#8216;1&#8217;)&#8221; &amp; firstname == &#8216;Ryan&#8217;} User.where{(&#8220;properties @&gt; (&#8216;male&#8217; =&gt; &#8216;1&#8217;)&#8221;)<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a717291747c76567bb0f086e15ae6e43?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nGilles<br \/>\nruby-on-rails postgresql arel<br \/>\nI&#8217;ve got some SQL that&#8217;s working when I want to look for particular pages that have the particular schemes assigned to it (all through the scheme_assignment):Page.find_by_sql(&#8220;SELECT pages.idFROM pagesINNER JOIN scheme_assignments ON (scheme_assignments.schemable_type = &#8216;Page&#8217; ANDscheme_assignments.schemable_id = pages.sid)INNER JOIN schemes ON (schemes.sid = scheme_assignments.scheme_id ANDschemes.sid IN (4,6,7))GROUP BY pages.id&#8221;)When I tried to convert this to ARel:Page.select(&#8220;pages.id&#8221;).joi<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0c660cb450f33249b6b23bcb84f0d63b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDylan Markow<br \/>\nruby ruby-on-rails-3 activerecord arel<br \/>\nI&#8217;m getting to grips with Rails 3 and I can&#8217;t seem to do a basic find from a result set. My code looks like the following:@project = @user.where({:projects =&gt; {:project_member_id =&gt; user.id}}).find_by_id(params[:id])I understand that the &#8220;where&#8221; section will not return a collection but merely create a query that is waiting to be run against the db. However, I can&#8217;t understand why I get the following error when I try to run the find_by_id:undefined method `to_sql&#8217; for #&lt;Arel::Attributes:<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/25ea5b35935ee51ddefbc70ac569158a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPierre Schambacher<br \/>\nsql ruby-on-rails-3 activerecord ruby-on-rails-3.2 arel<br \/>\nI have a few massive SQL request involving join across various models in my rails application. A single request can involve 6 to 10 tables.To run the request faster I want to use sub-queries in the joins (that way I can filter these tables before the join and reduce the columns to the ones I need). I&#8217;m trying to achieve this using ARel.I thought I found the solution to my problem there: How to do joins on subqueries in AREL within Rails But things must have changed because I get undefined method<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7504b530d1df33b972a3431d3640f618?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nChris Travers<br \/>\nruby-on-rails-3 arel ransack<br \/>\nI am using Ransack in my Rails 3.2.11 app and love it. I&#8217;m currently searching Profiles that match a given high school like so:&lt;%= f.check_box :profile_high_school_cont, {}, &#8220;#{current_user.profile.high_school}&#8221;, &#8221; %&gt;However I&#8217;d like to add some additional logic to this and further refine the search to Profiles that allow other users to find them by their high school.So I&#8217;m wondering if there is a way to add a check for whether a Profile&#8217;s :show_high_school is true in addition to whether<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5b84ccc2f7709bbb7105baeb40e0961b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nfl00r<br \/>\nruby ruby-on-rails-3 activerecord arel<br \/>\nWhenever i tried to useTable.create :a=&gt;&#8221;a&#8221;, :b=&gt;&#8221;b&#8221;ortab=Table.new tab.a=&#8221;a&#8221; tab.b=&#8221;b&#8221; tab.save!produces NoMethodError: undefined method `name&#8217; for nil:NilClass my table model is class Table &lt; ActiveRecord::Base set_table_name &#8220;table&#8221;set_primary_key &#8220;id&#8221; end<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ccce1fc2e623fada3f7d6ac8ab4ddfa8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nChris Keele<br \/>\nruby-on-rails ruby activerecord arel squeel<br \/>\nThe questionI&#8217;m trying to wrap my head around arel and squeel, but I feel like I lack the vocabulary to ask Google what I&#8217;m looking for.TL;DR: Does anyone know how to mimic Squeel&#8217;s Model.where{related.objects.field.matches string} syntax out of composed Squeel::Node objects?The problemThis question demonstrates how to build a Squeel KeyPath from a string, like so:search_relation = &#8216;person.pets&#8217; User.joins{Squeel::Nodes::KeyPath.new(search_relation.split(&#8216;.&#8217;))} # Mimics User.joins{person.pets}Th<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/20f35ed0d0a9754007d680d997307500?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\namd<br \/>\nsql ruby-on-rails-3 activerecord arel<br \/>\nI 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&#8217;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&#8217;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&#8217;s (part of) my controller:class<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a98a7a423d6a299385c6dc940fc540f4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAlan McCann<br \/>\nruby-on-rails ruby arel<br \/>\nI was implementing my first HABTM relationship and have run into an issue with my query. I am looking to validate my approach and to see if I have found a bug in the AREL (or some other part of Rails) code.I have the following modelsclass Item &lt; ActiveRecord::Basebelongs_to :userbelongs_to :categoryhas_and_belongs_to_many :regions endclass Region &lt; ActiveRecord::Basehas_ancestryhas_and_belongs_to_many :items endI have the associated items_regions table:class CreateItemsRegionsTable &lt; Ac<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>crispy ruby-on-rails activerecord arel scopes I have encountered some unexpected behavior in Active Record (3.2.13):There is a simple scope on my model:class User &lt; ActiveRecord::Basescope :verified, lambda { where(&#8216;verified = 1&#8217;) } endThis can be used fine on its own:User.verified.to_sql#=&gt; &#8220;SELECT \\&#8221;users\\&#8221;.* FROM \\&#8221;users\\&#8221; WHERE (verified = 1)&#8221;When I concatenate where-clauses, they are anded as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5758","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5758"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5758\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}