{"id":11101,"date":"2024-08-12T09:16:15","date_gmt":"2024-08-12T09:16:15","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11101"},"modified":"2024-08-12T09:16:15","modified_gmt":"2024-08-12T09:16:15","slug":"find-vs-find-by-vs-where","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/find-vs-find-by-vs-where","title":{"rendered":"Find vs Find_by vs Where"},"content":{"rendered":"<h3>Find<\/h3>\n<p>This method will find a single record or array of records from the table based on the arguments by comparing with primary key of the table (defaults to id) and returns it.If a record is not found, it will raise an ActiveRecord::RecordNotFound exception.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\"># record found\r\nUser.find(5)          =&gt; #&lt;User id: 5, name: \"Test\"&gt;\r\nUser.find(1, 5)      =&gt; [#&lt;User id: 1, name: \"Bob\"&gt;, #&lt;User id: 4, name: \"Test\"&gt;]\r\n\r\n# record not found\r\nUser.find(5)          =&gt; ActiveRecord::RecordNotFound (Couldn't find User with 'id'=5)\r\nUser.find(1, 5)      =&gt; ActiveRecord::RecordNotFound (Couldn't find all Users with 'id': (1, 5) \r\n          (found 1 results, but was looking for 2)\r\n<\/pre>\n<h3>Find_by<\/h3>\n<p>This is similar to find but this can find a record based on any column not only primary key (ID) of the table and returns the first record that matches a given condition. If a record is not found, nil is returned<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\n# record found\r\nUser.find_by(name: \u201cBob\u201d, location: \u201cIndia\u201d)   => #<User id: 1, name: \"Bob\", location: \u201cIndia\u201d>\r\n\r\n# record not found\r\nUser.find_by(name: \u201cBob\u201d, location: \u201cIndia\u201d)   => nil\r\n<\/pre>\n<h3>Where<\/h3>\n<p>where returns an ActiveRecord::Relation, which is a collection of model objects.If nothing matches the conditions, it simply returns an empty relation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\n# record found\r\nUser.where(name: \u201cBob\u201d, location: \u201cIndia\u201d)\r\n=> #<ActiveRecord::Relation [#<User id: 1, name: \"Bob\", location: \u201cIndia\u201d>,\r\n                                               #<User id: 15, name: \"Bob\", location: \u201cIndia\u201d>]...>\r\n# record not found\r\nUser.where(name: \u201cBob\u201d, location: \u201cIndia\u201d)   => #<ActiveRecord::Relation []>\r\n<\/pre>\n<p><strong>&#8211; Use whichever one you feel suits your needs best.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find This method will find a single record or array of records from the table based on the arguments by comparing with primary key of the table (defaults to id) and returns it.If a record is not found, it will raise an ActiveRecord::RecordNotFound exception. # record found User.find(5) =&gt; #&lt;User id: 5, name: &#8220;Test&#8221;&gt; User.find(1, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-11101","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11101"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11101"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11101\/revisions"}],"predecessor-version":[{"id":11104,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11101\/revisions\/11104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11102"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}