Archive for January 26, 2016
-
Query Objects in the Rails world - A Different Approach
January 26, 2016
If you have worked with Ruby on Rails before then you might be familiar with ActiveRecord
scopes
. Using them, you can achieve what many would consider very readable code. Let’s say that we have an application where we display an inbox where users receive messages.class Message < ActiveRecord::Base end
Now, let’s imagine that after reading a
Message
, it is marked as read, and let’s represent that with aread
column in the database. Additionally, our users can either archive theMessage
or move it to the trash. We’ll represent this concept with alocation
column in themessages
table.Querying the database the Rails way
Let’s say that our users want to have a way to view unread messages in their inbox. Using
ActiveRecord
, you could achieve…