{"id":3797,"date":"2015-04-22T19:22:57","date_gmt":"2015-04-22T23:22:57","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3797"},"modified":"2015-04-22T22:15:55","modified_gmt":"2015-04-23T02:15:55","slug":"ruby-on-rails-relationships-nested-resources-and-ajax-in-rails","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-relationships-nested-resources-and-ajax-in-rails\/","title":{"rendered":"Ruby on Rails: Relationships, Nested Resources, and AJAX in Rails"},"content":{"rendered":"<ul>\n<li>Most common relationship types for models\n<ul>\n<li>One-to-one<\/li>\n<li>One-to-many (parent\/child relationships)\n<ul>\n<li>The parent <strong>has many<\/strong> children<\/li>\n<li>The children <strong>belong to<\/strong> a parent<\/li>\n<\/ul>\n<\/li>\n<li>Many-to-many\n<ul>\n<li>A book <strong>has many<\/strong> authors<\/li>\n<li>An author <strong>has many<\/strong> books<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Create model in the middle so info can be reused\n<ul>\n<li>A doctor<strong> has many<\/strong> patients <strong>through<\/strong> appointments<\/li>\n<\/ul>\n<\/li>\n<li>Use the <span class=\"code\">resource<\/span> <strong>generator<\/strong> to generate migration, model, resource route, controller (no actions are generated), helper, assets (JavaScript or CoffeeScript \/ SCSS), tests &amp;\u00a0fixtures, but not any views\n<ul>\n<li>Migration adds\u00a0<span class=\"code\">t.references :book, index: true<\/span> which is a foreign key column translated into book_id<\/li>\n<li>Project example&#8230;\n<pre>bin\/rails g resource review name:string stars:integer comment:text book:references\r\nbin\/rake db:migrate<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Nested routes for resources accessed only in the context of a parent resource<\/li>\n<li>Associations are business logic, calculations, aggregations, and destruction\n<ul>\n<li>On model &#8230;\u00a0<span class=\"code\">Book.minimum(:price)<\/span><\/li>\n<li>On association &#8230;\u00a0<span class=\"code\">book.reviews.average(:stars)<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Use helpers for view logic to keep views clean<\/li>\n<li>When creating forms for nested routes, you must supply the parent and child objects in an array to the <span class=\"code\">form_for<\/span> method<\/li>\n<li>Review::STARS.each do |star| &#8230; the Review model has a stars constant (STARS = 1..5) &#8230; constants\u00a0should be all caps (except classes which are initial capped and camel-cased)<\/li>\n<li>When destroying a parent you need to remember to destroy the children too\n<ul>\n<li>Use the dependent option on associations to destroy the associated objects\n<ul>\n<li>Typically used on has_many associations<\/li>\n<li>destroy causes callbacks to run on child objects<\/li>\n<li>Other common values: delete_all, restrict<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">has_many :reviews, dependent: :destroy<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Extra queries impact performance! &#8230;\u00a0Avoid &#8220;N + 1&#8221; selects\n<ul>\n<li>Use includes to eagerly fetch associations<\/li>\n<\/ul>\n<\/li>\n<li><span class=\"code\">reviews.map(&amp;:stars)<\/span> &#8230;\u00a0is equivalent to&#8230;\u00a0<span class=\"code\">reviews.map { |r| r.stars }<\/span><\/li>\n<li>Unobtrusive JavaScript &#8230; separation of functionality from presentation<\/li>\n<li>Support for AJAX\u00a0in Rails &#8230; remote form, link, and button helpers\n<ul>\n<li>Handle different formats with <span class=\"code\">respond_to<\/span> and\/or <span class=\"code\">respond_with<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Turn off layouts for Ajax requests\n<ul>\n<li><span class=\"code\">layout -&gt;(controller) {controller.request.xhr? ? false : &#8216;application&#8217;}<\/span><\/li>\n<\/ul>\n<\/li>\n<li>Rails security\n<ul>\n<li>At a minimum, read the <a href=\"http:\/\/guides.rubyonrails.org\/security.html\" target=\"_blank\">Rails Security Guide<\/a> before releasing an app to the wild<\/li>\n<li>Get familiar with the <a href=\"https:\/\/www.owasp.org\/index.php\/Category:OWASP_Top_Ten_Project\" target=\"_blank\">OWASP Top 10<\/a><\/li>\n<\/ul>\n<\/li>\n<li>Deployment resources\n<ul>\n<li><a href=\"http:\/\/rubyonrails.org\/deploy\" target=\"_blank\">http:\/\/rubyonrails.org\/deploy<\/a><\/li>\n<li><a href=\"http:\/\/capistranorb.com\/\" target=\"_blank\">http:\/\/capistranorb.com\/<\/a><\/li>\n<\/ul>\n<\/li>\n<li>ActionMailer lets you send emails from your application<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Most common relationship types for models One-to-one One-to-many (parent\/child relationships) The parent has many children The children belong to a parent Many-to-many A book has many authors An author has many books Create model in the middle so info can be reused A doctor has many patients through appointments Use the resource generator to generate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3797","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3797"}],"version-history":[{"count":26,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3797\/revisions"}],"predecessor-version":[{"id":3831,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3797\/revisions\/3831"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}