arel

ActiveRecord builds instance of wrong class through a scope targeting an STI class

强颜欢笑 提交于 2020-01-04 06:44:17
问题 I would like to be able to call the build method on a scope that targets a certain class of model via its STI type, and have ActiveRecord build an instance of the correct class. class LineItem < ActiveRecord::Base scope :discount, where(type: 'DiscountLineItem') end class DiscountLineItem < LineItem; end > LineItem.discount.build # Expect an instance of DiscountLineItem here => #<LineItem ...> Here, I expected an instance of DiscountLineItem , not an instance of LineItem . 回答1: Even though

Eager-loading association count with Arel (Rails 3)

纵然是瞬间 提交于 2020-01-04 02:55:26
问题 Simple task: given that an article has many comments, be able to display in a long list of articles how many comments each article has. I'm trying to work out how to preload this data with Arel. The "Complex Aggregations" section of the README file seems to discuss that type of situation, but it doesn't exactly offer sample code, nor does it offer a way to do it in two queries instead of one joined query, which is worse for performance. Given the following: class Article has_many :comments

Finding unique records, ordered by field in association, with PostgreSQL and Rails 3?

混江龙づ霸主 提交于 2020-01-02 08:56:30
问题 UPDATE : So thanks to @Erwin Brandstetter, I now have this: def self.unique_users_by_company(company) users = User.arel_table cards = Card.arel_table users_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('min(cards.created_at)') end ... which seems to do exactly what I want. There are two shortcomings that I would still like

Matching nested model association attribute with includes

…衆ロ難τιáo~ 提交于 2020-01-02 04:08:31
问题 Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, name . I want to search for all posts with a given author "alice", by that author's name. Say there is another author "bob" who co-authored a post with alice. If I search for the first result using includes and where : post = Post.includes(:authors).where("authors.name" => "alice").first You'll see that the

How to properly add brackets to SQL queries with 'or' and 'and' clauses by using Arel?

六眼飞鱼酱① 提交于 2020-01-01 10:08:12
问题 I am using Ruby on Rails 3.2.2 and I would like to generate the following SQL query: SELECT `articles`.* FROM `articles` WHERE (`articles`.`user_id` = 1 OR `articles`.`status` = 'published' OR (`articles`.`status` = 'temp' AND `articles`.`user_id` IN (10, 11, 12, <...>))) By using Arel this way Article .where( arel_table[:user_id].eq(1) .or(arel_table[:status].eq("published")) .or( arel_table[:status].eq("temp") .and( arel_table[:user_id].in(10, 11, 12, <...>) ) ) ) it generates the following

Rails 3, RSpec 2.5: Using should_receive or stub_chain with named scopes

隐身守侯 提交于 2019-12-31 10:32:21
问题 I use Rails 3.0.4 and RSpec 2.5. In my controllers I use named scopes heavily, for example @collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate( :page => params[:page], :conditions => { ... }) In my tests, I want to be able to mock the result of such a query, not the wording . I do not think it makes sense to do something like GuestbookEntry.stub_chain(:nonreplies, :bydate, ...).and_return(...) because this test will fail the moment I decide to reorder the named scopes. With

How to use unscoped on associated relations in Rails3?

时光毁灭记忆、已成空白 提交于 2019-12-29 02:47:47
问题 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

Multiple CTE in single query

隐身守侯 提交于 2019-12-27 17:30:29
问题 Is it possible to combine multiple CTEs in single query with arel ? I am looking for way to get result like this: WITH 'cte1' AS ( ... ), WITH RECURSIVE 'cte2' AS ( ... ), WITH 'cte3' AS ( ... ) SELECT ... FROM 'cte3' WHERE ... As you can see, I have one recursive CTE and two non recursive. 回答1: Use the key word WITH once at the top. If any of your Common Table Expressions (CTE) are recursive (rCTE) you have to add the keyword RECURSIVE at the top once also, even if not all CTEs are recursive

Converting an SQL query to a Rails AREL query?

自作多情 提交于 2019-12-24 12:39:32
问题 I have a working SQL query thanks to the help of Erwin Brandstetter in my earlier question 'Order with a has_many relationship'. How would I turn this SQL into an ActiveRecords or AREL query for use in a scope? SELECT a.* FROM articles a LEFT JOIN ( SELECT DISTINCT ON (article_id) article_id, value FROM metrics m WHERE name = 'score' ORDER BY article_id, date_created DESC ) m ON m.article_id = a.id ORDER BY m.value DESC; The closest I have come is with a friends help... scope :highest_score,

rails: how do I build an active-relation scope to traverse many tables?

你。 提交于 2019-12-24 11:59:58
问题 I have these tables and relationships: user has_many projects project has_many tasks task has_many actions I would like to build a scope that allows me to select all of the current users actions, regardless of what project or task they belong to. Thanks 回答1: I don't think scopes are necessary for this if you use the nested_has_many_through plugin. class User < ActiveRecord::Base has_many :projects has_many :tasks, :through => :projects has_many :actions, :through => :tasks end class Project <