arel

What exactly is Arel in Rails 3.0?

前提是你 提交于 2019-11-27 08:59:56
问题 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? 回答1: 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

How to implement bulk insert in Rails 3

徘徊边缘 提交于 2019-11-27 07:06:58
I need to insert a array of emails as different records into my contacts table. How can this be done. Eg: @email = ["a@b.com", "c@d.com", "e@f.com", ... ] I dont want to use. @email.each do |email| @contact = Contact.new @contact.email = email @contact.save end This cause n insert quires. I just need a single insert query to insert these values. How can this be done in rails 3.0.9 (and ideally MySQL). Please help activerecord-import implements AR#import activerecord-import is a library for bulk inserting data using ActiveRecord. see how it works: books = [] 10.times do |i| books << Book.new(

Convert ActiveRecord habtm query to Arel

为君一笑 提交于 2019-11-27 06:16:20
问题 I have a pretty common habtm relationship: Photo has_and_belongs_to_many :tags Tag has_and_belongs_to_many :photos In my Photo model I've got a method "with tags" that I use to find a photo that is tagged with a given set of tag_ids. This query needs to match only photos that have all of the given tags, but disregarding the presence or lack of any other tags. Here's my method: def self.with_terms( array ) select('distinct photos.*').joins(:tags).where('tags.id' => array).group("photos." +

Nested queries in Arel

穿精又带淫゛_ 提交于 2019-11-27 05:25:37
问题 I am attempting to nest SELECT queries in Arel and/or Active Record in Rails 3 to generate the following SQL statement. SELECT sorted.* FROM (SELECT * FROM points ORDER BY points.timestamp DESC) AS sorted GROUP BY sorted.client_id An alias for the subquery can be created by doing points = Table(:points) sorted = points.order('timestamp DESC').alias but then I'm stuck as how to pass it into the parent query (short of calling #to_sql , which sounds pretty ugly). How do you use a SELECT

ActiveRecord query with alias'd table names

非 Y 不嫁゛ 提交于 2019-11-27 02:22:41
问题 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

Join the same table twice with conditions

微笑、不失礼 提交于 2019-11-27 01:37:08
问题 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) }

Ransack sort on count of HABTM or HMT associated records

会有一股神秘感。 提交于 2019-11-26 23:33:50
问题 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

Multiple CTE in single query

只谈情不闲聊 提交于 2019-11-26 22:13:10
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. 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: WITH RECURSIVE cte1 AS (...) -- can still be non-recursive , cte2 AS (SELECT ... UNION ALL SELECT ...) --

Subqueries in activerecord

蹲街弑〆低调 提交于 2019-11-26 19:44:30
With SQL I can easily do sub-queries like this User.where(:id => Account.where(..).select(:user_id)) This produces: SELECT * FROM users WHERE id IN (SELECT user_id FROM accounts WHERE ..) How can I do this using rails' 3 activerecord/ arel/ meta_where? I do need/ want real subqueries, no ruby workarounds (using several queries). Rails now does this by default :) Message.where(user_id: Profile.select("user_id").where(gender: 'm')) will produce the following SQL SELECT "messages".* FROM "messages" WHERE "messages"."user_id" IN (SELECT user_id FROM "profiles" WHERE "profiles"."gender" = 'm') (the

How to implement bulk insert in Rails 3

一个人想着一个人 提交于 2019-11-26 17:37:30
问题 I need to insert a array of emails as different records into my contacts table. How can this be done. Eg: @email = ["a@b.com", "c@d.com", "e@f.com", ... ] I dont want to use. @email.each do |email| @contact = Contact.new @contact.email = email @contact.save end This cause n insert quires. I just need a single insert query to insert these values. How can this be done in rails 3.0.9 (and ideally MySQL). Please help 回答1: activerecord-import implements AR#import activerecord-import is a library