{"data":{"wordpressPost":{"id":"3651fc2f-4205-50e6-85f4-694df35919e4","title":"Beginning Programming: Active Record in Ruby/Rails","slug":"beginning-programming-active-record-rubyrails","content":"<div id=\"zen-preamble\" role=\"article\">\n<p><img class=\"size-medium wp-image-277 alignright\" alt=\"active record\" src=\"http://helveticajessica.com/wp-content/uploads/2014/07/active-record-300x215.jpg\" width=\"300\" height=\"215\" srcset=\"http://helveticajessica.com/wp-content/uploads/2014/07/active-record-300x215.jpg 300w, http://helveticajessica.com/wp-content/uploads/2014/07/active-record.jpg 863w\" sizes=\"(max-width: 300px) 100vw, 300px\" />The goal of this article is to introduce the concepts behind ActiveRecord and the active record pattern to beginning programmers so that they can go on to successfully complete beginning tutorials without being confused by the terminology, etc.</p>\n<h3>What is ActiveRecord?</h3>\n<p>&#8220;Active record pattern&#8221; is an <a title=\"A Ruby Newb’s Intro to Classes and Object-Orientedness\" href=\"http://helveticajessica.com/ruby-newbs-intro-classes-object-orientedness/\">object-oriented paradigm</a> for relational databases. (Nothing here will make sense if you don&#8217;t have any idea what that means, so <a title=\"A Ruby Newb’s Intro to Classes and Object-Orientedness\" href=\"http://helveticajessica.com/ruby-newbs-intro-classes-object-orientedness/\">go read up</a> if you need to and then come back here when you&#8217;re done.)</p>\n<p>Objects contain data and methods that can act on that data, so in your database there is a Class for the table, and an instance of that Class is a row in the table. The row/object has both data and some functions that can be used to act on that data.</p>\n<p>ActiveRecord is a Ruby library that uses the active record pattern. It comes with the Ruby on Rails framework but is also available as a gem and can be used independently of Rails. It gives you access to handy methods for your Class/table to inherit and use, so you can easily interact with your database in an object-oriented way.</p>\n<h3>How it works/Examples</h3>\n<p>&#8220;Class methods are used to perform table-level operations, and instance methods perform operations on the individual rows.&#8221; &#8211; <a href=\"http://rubylearning.com/satishtalim/ruby_activerecord_and_mysql.html\">RubyLearning.com</a></p>\n<p>In the example below, we define the table <code>users</code> as the Class, <code>User</code>. We also define the table <code>things</code> as the Class <code>Thing</code>.</p>\n<p>We make both classes children of ActiveRecord&#8217;s <code>Base</code> class so that they can inherit methods like <code>primary_key</code> and <code>column_for_attribute</code> (and more!), which define the primary key field and return the column object for the named attribute, respectively.</p>\n<p>&nbsp;</p>\n<blockquote>\n<pre>\tclass User &lt; ActiveRecord::Base\r\n\t  has_many :things\r\n\tend\r\n\r\n\tclass Thing &lt; ActiveRecord::Base\r\n\t  belongs_to :user\r\n\tend\r\n\r\n\tjames = user.create(:name =&gt; 'James')</pre>\n</blockquote>\n<p>&nbsp;</p>\n<p>As you might have guessed, <code>Base</code> also includes the methods <code>has_many</code> and <code>belongs_to</code>, which help to define the relationships between tables in your database.</p>\n<p>And finally, we have created <code>james</code> as an instance of the class <code>User</code>, and &#8216;James&#8217; will go in the <code>:name</code> column of the <code>users</code> table.</p>\n<p>There is a lot more to do in order to make this code work, including defining the tables themselves (imagine that!), which you can easily learn about in one of many tutorials available on the web. I&#8217;ve included a couple of tutorials below.</p>\n<h3>Pros</h3>\n<p>By wrapping up data with the methods that can interact with them, the active record pattern informs the programmer as to what can be done with that data.</p>\n<p>Inheritance increases functionality by creating access to a whole host of useful functions that do not have to be defined every time you create a new table/Class.</p>\n<h3>Cons</h3>\n<p>When you go to test your code to make sure it works, if your data and your methods are all wrapped together it is hard to test your functions without a database! To get around this you may need to create a testing database or pass your data as an argument. (See also <a href=\"http://jgaskins.org/blog/2012/04/20/data-mapper-vs-active-record/\" target=\"new\">Data Mapper vs Active Record</a>.)</p>\n<h3>Alternatives</h3>\n<p>A similar-yet-different option to ActiveRecord in Ruby is <a href=\"http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html\" target=\"new\">Sequel</a>, which is newer to the scene yet offers a few features developers might find appealing, such as a decreased need to use raw SQL in your queries.</p>\n<p>Of course, Ruby isn&#8217;t the only language with libraries to implement active record architecture. Listing them all isn&#8217;t the goal of this article, but a quick Google search or look on Wikipedia will help you to learn more about them.</p>\n<p><a href=\"http://martinfowler.com/eaaCatalog/activeRecord.html\" target=\"new\">Active record</a> isn&#8217;t the only object-oriented approach to relational databases, though. The data mapper pattern &#8220;moves data between objects and a database while keeping them independent of each other and the mapper itself.&#8221; (<a href=\"http://martinfowler.com/eaaCatalog/dataMapper.html\" target=\"new\">Martin Fowler</a>)</p>\n<p>Objects created with an active record approach contain methods including create, read, update, and delete (<a href=\"http://en.wikipedia.org/wiki/Create,_read,_update_and_delete\" target=\"new\">CRUD</a>). But objects created with a data mapper pattern, have a separate &#8220;layer&#8221; that takes care of those actions. This relieves some of the pain of testing, as it allows you to more easily test your non-CRUD methods.</p>\n</div>\n<div id=\"zen-supporting\" role=\"main\">\n<div id=\"zen-explanation\" role=\"article\">\n<h3>Expand your learning</h3>\n<p>This article cannot possibly cover all the ins and outs of active record pattern and ActiveRecord, but should give give you enough of a beginner&#8217;s understanding to go out and learn more on your own!</p>\n</div>\n<div id=\"zen-participation\" role=\"article\">\n<h4>Sources/Additional Reading:</h4>\n<p><a href=\"http://en.wikipedia.org/wiki/Active_record_pattern\" target=\"new\">Wikipedia: Active Record Pattern</a><br />\n<a href=\"http://en.wikipedia.org/wiki/Object-relational_mapping\" target=\"new\">Wikipedia: Object-Relational Mapping</a><br />\n<a href=\"http://rubylearning.com/satishtalim/ruby_activerecord_and_mysql.html\" target=\"new\">ActiveRecord and MySQL</a><br />\n<a href=\"http://www.integralist.co.uk/posts/sqlite-and-activerecord/\" target=\"new\">SQLite and ActiveRecord</a><br />\n<a href=\"http://guides.rubyonrails.org/active_record_basics.html\" target=\"new\">Rails: Active Record Basics</a><br />\n<a href=\"http://guides.rubyonrails.org/active_record_querying.html\" target=\"new\">Rails: Active Record Querying</a><br />\n<a href=\"http://jgaskins.org/blog/2012/04/20/data-mapper-vs-active-record/\" target=\"new\">Data Mapper vs Active Record<br />\n</a><a title=\"A Ruby Newb’s Intro to Classes and Object-Orientedness\" href=\"http://helveticajessica.com/ruby-newbs-intro-classes-object-orientedness/\">Ruby Newb&#8217;s Intro to Classes and Object-Orientedness</a></p>\n</div>\n</div>\n","date":"July 27, 2014","categories":[{"name":"ones and zeros","slug":"ones-and-zeros"}],"tags":null,"author":{"name":"jessica","slug":"jessica"}}},"pageContext":{"id":"3651fc2f-4205-50e6-85f4-694df35919e4"}}