The question is: how can we retrieve a owners class of a polymorphic association without loading the owner object into memory? Solution There are (at least) two ways to do this. First one is not recommended (as it might fail with multiple namespaces with same class names, etc: or a better one that will work […]
Read more at the sourceCategory Archives: ActiveRecord
Integrating Trailblazer and ActiveRecord transactions outside of a request lifecycle
When you use Ruby on Rails with ActiveRecord, you can get used to having separate transaction on each request. This is valid also when using Trailblazer (when inside of a request scope), however Trailblazer on its own does not provide such functionality. It means that when you’re using it from the console and/or when you […]
Read more at the sourceActiveRecord isolated, namespaced scopes and methods using instance_exec and ActiveSupport::Concern
Rails concerns are a really good way to provide shared functionalities into your models. For example you can add scopes and then apply them in multiple classes: One of the downsides of this solution is a “models pollution”. Every model in which we want to include our module, will get all the methods and scopes […]
Read more at the sourceExtending ActiveRecord association to cleanup your associations code
For some reason not many RoR developers know that they can extend ActiveRecord associations. This feature can be a great way to cleanup any relational code that is dependent on a parent resource (or to just add a simple functionalities to scopes). Here’s a really simple example how you can use it: That way you […]
Read more at the sourceRuby on Rails and Dragonfly processed elements tracking with two layer cache – Memcached na ActiveRecord underneath
Dragonfly is a great on-the-fly processing gem, especially for apps that rapidly change and need files processed by multiple processors (for example, when you need to generate many thumbnails for the same attached image). Since it is a on-the-fly processor, it has some downsides, and the biggest one, in my opinion, is keeping track of […]
Read more at the sourceActiveRecord count vs length vs size and what will happen if you use it the way you shouldn’t
One of the most common and most deadly errors you can make: using length instead of count. You can repeat this multiple times, but you will always find someone who’ll use it the way it shouldn’t be used. So, first just to make it clear: #count – collection.count Counts number of elements using SQL query […]
Read more at the sourceActiveResource relations – a bit of magic to make it look and feel more like ActiveModel relations
ActiveResource collection new problem ActiveResource can be pretty helpful when you have a RESTful JSON API. Although it has some limitations. One of the most irritating is a lack of nested resources new scope method. When you have structure like this: You can do some basic stuff: But unfortunately if you try something like this: […]
Read more at the sourceState Machine gem and Rails (ActiveModel) 4.1 – NoMethodError – protected method ‘around_validation’ called for StateMachine
State machine is a great gem, unfortunately it’s not working with Rails 4.1. If you upgrade your app and try to use it, you’ll end up with following error: Here’s a fix. Just put it into your initializers: Note: It works also for any non-Rails, ActiveModel based apps.
Read more at the sourceCustom model callbacks in RubyOnRails
RubyOnRails provides us with many model callbacks around the object’s lifecycle when object is being created, updated or destroyed. For example: before_create, after_create, before_update, after_destroy etc. We use them to write & run our code around the object’s lifecycle by … Continue reading →
Read more at the sourceRuby (Rails, Sinatra) background processing – Reentrancy for your workers is a must be!
Background processing is a must-be in any bigger project. There’s no way to process everything in a lifetime of a single request. It’s such common, that I venture to say that each and every one of us made at least one background worker. Today I would like to tell you a bit about reentrancy. What […]
Read more at the source