Ruby on Rails

Summarized using AI

Active Record Can't Do It? Arel can!

Vipul A M and Prathamesh S • January 10, 2015 • Earth

The video titled "Active Record Can't Do It? Arel Can!" presented by Vipul A Prathamesh S at the Garden City Ruby 2015 conference emphasizes the importance of Arel, a powerful library for generating SQL queries in Ruby applications. Although Active Record is an excellent ORM that handles a majority of database interactions, this talk highlights scenarios where Active Record may not suffice, paving the way for Arel's robust capabilities. The main points discussed are as follows:

  • Introduction to Arel: Arel is a Ruby library designed for SQL generation based on relational algebra, making complex queries easier to construct and manage.
  • Active Record vs. Arel: While Active Record is a full-fledged ORM handling query execution, Arel focuses on SQL generation, which allows developers to build queries without being tied to a specific database structure.
  • Situation Where Arel Excels: In situations requiring complex SQL queries—such as filtered bookings, ratings, and self-joins—Arel simplifies the process by allowing developers to focus on conditions and relationships without worrying about performance issues associated with raw SQL.
  • Practical Examples: The presenters share a real-world application they developed called Backpackers, which utilizes Active Record for basic database operations but manages complex SQL using raw queries and Arel for improved performance and flexibility.
  • Arel's Features: Arel offers various predicates and support for complex operations, including inner and outer joins, allowing developers to execute structured queries efficiently and maintainable code.
  • Conclusion and Encouragement: The presenters encourage developers to integrate Arel into their Rails applications, highlighting its database-agnostic nature and versatility, enabling more powerful and maintainable database interactions. They conclude by inviting attendees to experiment with Arel and explore the code available on GitHub.

Overall, the talk seeks to empower Ruby developers to leverage Arel for more effective SQL management, especially when Active Record falls short, thus unlocking greater potential for database manipulation while maintaining a clear and object-oriented approach to code.

Active Record Can't Do It? Arel can!
Vipul A M and Prathamesh S • January 10, 2015 • Earth

By, Vipul A Prathamesh S

ActiveRecord can't do it? Arel can! Active Record is awesome. But how does ActiveRecord handle generating complex SQL queries? Under the hood it's handled by Arel. Most of the time, Rails developers don't have to know about how Arel works. But sometimes Active Record can't satisfy our needs. Also Arel has many strengths not exposed through Active Record. Let's experiment with Arel directly and wield great SQL power in database agnostic way. Oh and did I mention, this is a fun talk, even for us, to “perform”.

Help us caption & translate this video!

http://amara.org/v/GF2k/

Garden City Ruby 2015

00:00:14.799 Hello everyone, good morning! Nice to see you all here today. We are excited to present our talk titled "Active Record Can't Do It? Arel Can!".
00:00:21.240 Hopefully, some of you might already be familiar with Arel. Can anyone here tell me what Arel is? Great! How many of you know about Rails? Nice!
00:00:38.480 I am Vipul, and this is Prathamesh. We are supposed to introduce ourselves in the other order, but that’s fine. We work for a company called BigBinary, which is based out of Miami. It’s a remote company, and we work from different locations. For instance, I work from home.
00:01:01.719 We create a lot of videos, so you can check out our website at bigbinary.com. We also provide many tutorials. We are currently looking for people, so if you're interested, take a look at bigbinary.com/jobs for more information.
00:01:17.159 I don't know if anyone officially invited you to the conference, but welcome to the second conference! I’m not entirely sure about the name, but we hope you stick with us until the end because it's a long name and I'm not sure how to pronounce it.
00:01:40.119 So, we happen to live in a place called Pune, which is near Mumbai. Just a week ago, we started our journey and it took us about 167 hours to get here. We traveled over there on foot, and it was quite leisurely.
00:02:01.560 Back in Pune, we regularly hold meetups and various events under Pune.rb. If you’re ever around, feel free to join us. Last year, we started the Pune RubyConf, which is a one-day regional Ruby conference slightly inclined towards fun.
00:02:25.879 At this conference last year, we launched something called Ruby India. How many of you have heard of Ruby India? Quite a few!
00:02:48.560 Ruby India aims to highlight content from the Ruby Indian community. We share links for submitting CFPs and information about various meetups. We also encourage you to subscribe to Ruby India, as our next issue will be published soon.
00:03:26.760 In the last RubyConf, we also announced a podcast called "Ru on Ruby India," where we interview many individuals from the Indian community. Recently, we interviewed C42 representatives and others, so do check it out.
00:03:50.400 We like to connect with members of the Indian Ruby community to discuss how it has developed over the years. However, I must clarify that this discussion isn’t about that.
00:04:00.239 Now, before we dive into the main presentation, I want to share a bit about what Prathamesh has been up to recently. He has been working on some pretty exciting projects, including a startup called Backpackers.
00:04:25.879 As part of this startup, he travels a lot, and since his job allows for remote work, he enjoys meeting many friends who are also backpackers! That's how he came up with the name Backpackers.
00:04:41.440 Backpackers aims to help friends with basic task management, like finding good locations, restaurants, and accommodation options while traveling. We know that finding good food is essential, and we want to assist users with suggestions for affordable hostels and hotels.
00:05:03.160 These are all basic tasks that many travel apps cover. But what makes Backpackers unique is it includes magical features, such as a function for tracking lost luggage. If you have ever dealt with lost bags, you know how useful that can be!
00:05:30.080 In fact, we have had many happy customers find their bags after searching for two weeks. Our customers are amazed by how these features work. Let me give you an overview of my application. It is powered by Active Record, which helps manage travelers, reviews, bookings, and locations.
00:06:33.920 The traveler class manages reviews and bookings, and I’ve organized tasks related to specific locations. This system helps provide detailed information, including reviews and bookings for hostels or nearby locations, through self-joins.
00:06:57.959 My application is actually up and running, and I encourage you to visit it at Backpackers.heroku.com. I cannot guarantee if it’s live at this moment, but it’s in production with over 31 users!
00:07:15.560 The number may seem small, but maintaining performance with 31 users on a single instance is crucial. I depend on raw SQL querying since Active Record can be somewhat slower for my needs.
00:07:30.960 With Active Record, things can get complex quickly, especially when writing SQL queries for searching various cuisines while traveling in another country. Finding Indian cuisine can be particularly challenging without the right queries.
00:08:05.280 That’s where raw SQL queries come into play, allowing for accurate searches. However, using raw queries might introduce a few issues, such as being database-specific, which can lead to problems if you decide to switch databases later.
00:08:26.080 This is where Arel really shines. Have you heard about Arel? Arel is a Ruby library designed for generating SQL. It is based on relational algebra and employs concepts like abstract syntax trees to store query information in a structured manner.
00:09:03.760 Behind the scenes, Arel is utilized within Active Record to manage queries. It's been part of Rails since version 3, but it offers additional features that aren't exposed through the Active Record API, often leading to confusion.
00:09:49.919 While Active Record serves as a full-fledged ORM that manages query execution and conversion to Ruby objects, Arel focuses solely on generating SQL. Therefore, it doesn't need to know details about the database structure, just the SQL generation.
00:10:41.440 To get started with Arel in your Rails application, all you need to do is access the underlying Arel table object associated with your model. Arel provides a very convenient method that allows you to build queries based on table attributes.
00:10:30.080 For instance, if you want to find records based on specific conditions, Arel allows you to create clear and concise queries that align with your needs. This abstraction also means your queries become database-agnostic.
00:11:05.080 You can easily support multiple databases, such as MySQL or PostgreSQL, without changing the underlying query much. Arel offers various predicates to help you define conditions when retrieving records, making it a robust option.
00:12:25.600 For operations like finding vegetarian food, you can use different Arel predicates such as 'does not match'. Arel also supports a range of comparison options, enhancing its versatility.
00:13:04.800 Now, if we want to handle more complex queries, such as finding locations with specific conditions or joining tables based on attributes, Arel can facilitate that too.
00:13:30.560 To illustrate this, let's say we want to find locations with bookings and ratings above a certain threshold. Arel drastically simplifies the process, allowing you to specify join conditions, have additional WHERE clauses, and select only the needed attributes.
00:14:10.160 In Arel, both inner and outer joins can be executed seamlessly. This will help in getting structured queries efficiently while allowing for clear and maintainable code.
00:15:10.400 With Arel, you can also approach various structured queries, such as self-joins, which are useful when searching among the same table. For example, if a friend wanted hotels within a certain distance, Arel can effortlessly handle this.
00:15:45.600 By using table aliasing, you can distinguish between two instances of the same table, enabling you to write complex queries that get exactly what you're looking for without compromising on performance.
00:16:15.440 This allows you to leverage Arel’s robust capabilities to derive queries tailored to your application’s requirements while maintaining database agnosticism.
00:17:04.160 In summary, Arel is an excellent tool for Ruby developers looking to harness the full power of SQL, especially when dealing with complex conditions and relationships among your data.
00:17:50.560 Today, we went over many aspects of Arel, such as working with various predicates, composing queries, managing associations, and keeping things flexible and maintainable in your applications.
00:18:25.560 Using Arel alongside Active Record offers a more object-oriented way of handling your database operations, which is powerful for modern applications.
00:19:10.880 So, thank you for joining our session today. We hope you found it insightful and that you feel encouraged to use Arel within your Rails applications.
00:19:45.040 Our code is available on GitHub, and we encourage you to experiment with Arel and see how it can enhance your workflow. Thank you!
Explore all talks recorded at Garden City Ruby 2015
+8