{"id":815,"date":"2013-12-07T14:47:18","date_gmt":"2013-12-07T09:17:18","guid":{"rendered":"http:\/\/www.allerin.com\/blog\/?p=815"},"modified":"2013-12-27T18:43:13","modified_gmt":"2013-12-27T13:13:13","slug":"ruby-on-rails-and-database","status":"publish","type":"post","link":"https:\/\/www.allerin.com\/blog\/ruby-on-rails-and-database\/","title":{"rendered":"Ruby on Rails and Database"},"content":{"rendered":"<p>Ruby on Rails is a Web application framework used for developing Web applications. If you expect or want a user to enter information through a Web form, you require a database to store that information. In Rails framework, the database table has a plural name (ending with \u2018s\u2019), and the primary key in the database is known as id and auto-incremented. To retrieve stored information from the database, Rails uses a component named ActiveRecord that functions as a bridge between the database and Ruby code. ActiveRecord:<\/p>\n<p>ActiveRecord is an Object\/Relational Mapping layer available with Rails. It follows standard ORM rules such as<\/p>\n<ul class=\"list-content\">\n<li>Columns map to object attributes<\/li>\n<li>Rows map to objects and<\/li>\n<li>Tables map to classes<\/li>\n<\/ul>\n<p>Each ActiveRecord object has Create, Read, Update and Delete methods for database access. This characteristic allows Ruby on Rails applications to have straightforward mappings between applications objects and database tables. ActiveRecord does away with the need to use SQL in most cases. It is compatible with different databases such as MySQL, SQLite, and PostgreSQL. Irrespective of the database you are using, ActiveRecord method format remains the same. Ruby on Rails has well-defined relationships that reduce the need of writing low-level queries for the database. Let&#8217;s learn how to use ActiveRecord to perform queries to database. Working with the Database in Rails<\/p>\n<p>Retrieve objects from the database:<\/p>\n<p>ActiveRecord has many finder methods. These methods allow querying of database by passing arguments. The finder methods eliminate the need to write raw SQL for the purpose. Some of the finder methods are:<\/p>\n<ul class=\"list-content\">\n<li>from<\/li>\n<li>having<\/li>\n<li>including<\/li>\n<li>joins<\/li>\n<li>none<\/li>\n<li>select<\/li>\n<li>where<\/li>\n<\/ul>\n<p>The above finder methods return an instance of ActiveRecord:Relation Retrieve a single object:<\/p>\n<p>ActiveRecord presents you with five different ways to retrieve a single object:<\/p>\n<ul class=\"list-content\">\n<li>Using a primary key:<\/li>\n<\/ul>\n<p>The <em>Model.find(primary_key)<\/em> retrieves an object corresponding to the value of the primary key.<\/p>\n<p>For example<br \/>\n<em>client = Client.find(10)<\/em> <strong>find client with primary id=10<\/strong> If the <em>Model.find(primary_key)<\/em> does not find primary key with the corresponding value 10, it will return an instance of ActiveRecord::RecordNotFound<\/p>\n<ul class=\"list-content\">\n<li>Take:<\/li>\n<\/ul>\n<p><em>Model.take<\/em> retrieves an object without any conditional parameters or primary key <em>client = Client.take<\/em><\/p>\n<p>It will retrieve the object in the table with id=1. The retrieved object will vary with the engine used. <em>Model.take<\/em> will return Nil if no record is found in the table<\/p>\n<ul class=\"list-content\">\n<li>First:<\/li>\n<\/ul>\n<p><em>Model.first<\/em> retrieves the first record with primary key <em>client = Client.first<\/em><\/p>\n<p>Model.first will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record<\/p>\n<ul class=\"list-content\">\n<li>Last:<\/li>\n<\/ul>\n<p><em>Model.last<\/em> retrieves the last record with primary key<em> client = Client.last<\/em><\/p>\n<p><em>Model.last<\/em> will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record<\/p>\n<ul class=\"list-content\">\n<li>Find_by!:<\/li>\n<\/ul>\n<p><em>Model.find_by!<\/em> Searches for the first record in the table matching some conditions <em>Client.find_by! first_name: &#8216;Harry&#8217;<\/em><strong> it will retrieve a record which has Harry in the first_name field.<\/strong> <em>Model.find_by!<\/em> will raise an instance of ActiveRecord::RecordNotFound, if it does not find a matching record Types of Databases on Rails:<\/p>\n<p>Ruby on Rails recommends the creation of three databases development, production and test. As per the convention their name should be<\/p>\n<ul class=\"list-content\">\n<li>library_development<\/li>\n<li>library_production<\/li>\n<li>library_test<\/li>\n<\/ul>\n<p>Before using the database, you need to initialize them and create a username and password for each database having full read and write permissions. The information about the databases is written in database.yml file located in the C:\\ruby\\library\\config<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Rails, ActiveRecord works as a bridge between the database and Ruby code and allows straightforward mappings between applications objects and database tables. ActiveRecord is compatible with databases like MySQL, SQLite and PstegreSQL. RoR recommends three separate databases for development, production and test environments.<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[4],"tags":[97,20],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-my-voice","tag-database","tag-ruby-on-rails"],"_links":{"self":[{"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":7,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":826,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/posts\/815\/revisions\/826"}],"wp:attachment":[{"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.allerin.com\/blog\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}