<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cesar Daniel Diaz Jolibois on Medium]]></title>
        <description><![CDATA[Stories by Cesar Daniel Diaz Jolibois on Medium]]></description>
        <link>https://medium.com/@diazcd85?source=rss-a921f4ffbc23------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*W2doNy8NsjkTjsm3</url>
            <title>Stories by Cesar Daniel Diaz Jolibois on Medium</title>
            <link>https://medium.com/@diazcd85?source=rss-a921f4ffbc23------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 13:43:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@diazcd85/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My First JSON Api on Rails]]></title>
            <link>https://medium.com/@diazcd85/my-first-json-api-on-rails-b54307f1d0a6?source=rss-a921f4ffbc23------2</link>
            <guid isPermaLink="false">https://medium.com/p/b54307f1d0a6</guid>
            <dc:creator><![CDATA[Cesar Daniel Diaz Jolibois]]></dc:creator>
            <pubDate>Mon, 03 Dec 2018 15:20:06 GMT</pubDate>
            <atom:updated>2018-12-03T15:20:06.559Z</atom:updated>
            <content:encoded><![CDATA[<p>Let me give you some background context first. Our first solo project assignment for Backend Module 3 at Turing was the Rails Engine. An Api (Application Program Interface) that will return information from our database in a JSON (JavaScript Object Notation )format using a RESTful architecture.</p><p>Before hand I had to apologize if some of writing don’t make any sense, English is my second language and I thought writing this type of technical blogs in the long run will help me to overcome my fear of been expose and second my writing skills. So i’m sorry for my grammatical errors (more like horrors).</p><p>Usually I the type of person who just jump in to the sea and them figured out where to swim, but this project I wanted to approach it in a more have everything you are goin to need before to go approach.</p><h3>Code snippet of the Schema.rb</h3><pre>ActiveRecord::Schema.define(version:) do</pre><pre>  enable_extension &quot;plpgsql&quot;<br>  enable_extension &quot;citext&quot;</pre><pre>create_table &quot;customers&quot;, force: :cascade do |t|<br>    t.citext &quot;first_name&quot;<br>    t.citext &quot;last_name&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>  end</pre><pre>create_table &quot;invoice_items&quot;, force: :cascade do |t|<br>    t.bigint &quot;item_id&quot;<br>    t.bigint &quot;invoice_id&quot;<br>    t.integer &quot;quantity&quot;<br>    t.integer &quot;unit_price&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>    t.index [&quot;invoice_id&quot;], name: &quot;index_invoice_items_on_invoice_id&quot;<br>    t.index [&quot;item_id&quot;], name: &quot;index_invoice_items_on_item_id&quot;<br>  end</pre><pre>create_table &quot;invoices&quot;, force: :cascade do |t|<br>    t.bigint &quot;customer_id&quot;<br>    t.bigint &quot;merchant_id&quot;<br>    t.string &quot;status&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>    t.index [&quot;customer_id&quot;], name: &quot;index_invoices_on_customer_id&quot;<br>    t.index [&quot;merchant_id&quot;], name: &quot;index_invoices_on_merchant_id&quot;<br>  end</pre><pre>create_table &quot;items&quot;, force: :cascade do |t|<br>    t.string &quot;name&quot;<br>    t.text &quot;description&quot;<br>    t.integer &quot;unit_price&quot;<br>    t.bigint &quot;merchant_id&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>    t.index [&quot;merchant_id&quot;], name: &quot;index_items_on_merchant_id&quot;<br>  end</pre><pre>create_table &quot;merchants&quot;, force: :cascade do |t|<br>    t.citext &quot;name&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>  end</pre><pre>create_table &quot;transactions&quot;, force: :cascade do |t|<br>    t.bigint &quot;invoice_id&quot;<br>    t.string &quot;credit_card_number&quot;<br>    t.string &quot;credit_card_expiration_date&quot;<br>    t.string &quot;result&quot;<br>    t.datetime &quot;created_at&quot;, null: false<br>    t.datetime &quot;updated_at&quot;, null: false<br>    t.index [&quot;invoice_id&quot;], name: &quot;index_transactions_on_invoice_id&quot;<br>  end</pre><pre>add_foreign_key &quot;invoice_items&quot;, &quot;invoices&quot;<br>  add_foreign_key &quot;invoice_items&quot;, &quot;items&quot;<br>  add_foreign_key &quot;invoices&quot;, &quot;customers&quot;<br>  add_foreign_key &quot;invoices&quot;, &quot;merchants&quot;<br>  add_foreign_key &quot;items&quot;, &quot;merchants&quot;<br>  add_foreign_key &quot;transactions&quot;, &quot;invoices&quot;<br>end</pre><p>I spend a several hours planning and building Model relations, thinking strategies to make my SQL queries easy to implement, double check every line of code I wrote including my tests. At the end I spend to long on planning or falling into rabbit holes when I had and idea on how make something dryer, or better, but usually this meant time spend on research and less time to produce actual code.</p><h4>The Challenge</h4><p>beyond highly complex ActiveRecord queries or a super volatile spec harness was balancing life and be in a position were you can produce useful code was the challenge. As and immigrant I prioritize my family over everything, and last week several of my direct relatives were in really difficult situations, what made me take the eye away from the screen and pick up phone and do what I could to help.</p><h4>Room to improvement</h4><p>Time management is one of the situations were I need to improve, know when to struggle or for how long and pursuit a particular path and for how long stay on a particular path before realize that probably are easier solutions are situations were in need to get better at. I don’t see the time a spend on preparations at the beginning of the project as a bad thing, just try to be more efficient with time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b54307f1d0a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails Generator (AKA Rails Magic).]]></title>
            <link>https://medium.com/@diazcd85/rails-generator-aka-rails-magic-1c42daaa4b75?source=rss-a921f4ffbc23------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c42daaa4b75</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Cesar Daniel Diaz Jolibois]]></dc:creator>
            <pubDate>Wed, 07 Nov 2018 20:44:36 GMT</pubDate>
            <atom:updated>2018-11-07T20:44:36.270Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple weeks ago I was introduce to the concept of Magic in Rails, at the beginning, like most people, I was skeptical about the myths about what it could do. This popular framework in fact allows you to create a web application in a few hours from scratch but, again, the idea that some piece of code could do so much is like something from a children’s book of wizards and alchemists.</p><p>Rails magic refers to a broad set of instructions deep in the Ruby on Rails code that facilitates the creation of files, relations between tables in the data base, security, forms and tags for views, and much more functionality (honestly I only have learned a fractions of Rails’ power) to help you implement your designs. All of this happens under Rails’ hood and most of the time we don’t have a clue of what’s going on, but it definitely make our lives easier.</p><p>In this section I will be talking about Rails Generator and how to create some basic functionality using MVC design architecture.</p><ol><li>Models</li></ol><p>Creating Models using rails g command in your shell (g is an abbreviation for generate) follows this type of syntax:</p><pre>rails g model &lt;model_name&gt; &lt;column_1&gt;:data_type &lt;column_2&gt;:data_type:option</pre><p>An example would be:</p><pre>rails g model User first_name:string{40} last_name:string{40} age:integer email:string:uniq</pre><p>Looking at this expression, we can understand the following:<br>We are calling the rails generate routine for models, capitalizing the tables’ name for conventions, and then proceeding to add columns and data types, separated by colons. Also, we can give special attributes to the columns; this will be covered with more depth in the data type section.</p><p>This action will generate these files:</p><pre>create db/migrate/20181103175426_create_users.rb<br>create app/models/user.rb</pre><p>The content of the migration file:</p><pre><br> ./db/migrate/20181103175426_create_users.rb</pre><pre>class CreateUsers &lt; ActiveRecord::Migration[5.1]<br> def change<br>   create_table :users do |t|<br>     t.string :first_name, limit: 40<br>     t.string :last_name, limit: 40<br>     t.integer :age<br>     t.string :email</pre><pre>     t.timestamps<br>   end<br>   add_index :users, :email, unique: true<br> end<br>end</pre><h4>2. Data types and Options</h4><p>The following is a list with data types and a short explanation. These posts were the inspiration for this article: <a href="https://richonrails.com/articles/rails-model-generator-shortcuts">https://richonrails.com/articles/rails-model-generator-shortcuts</a> , <a href="https://medium.com/@kevinyckim33/rails-generators-cli-cheatsheet-711295e7a1ed">https://medium.com/@kevinyckim33/rails-generators-cli-cheatsheet-711295e7a1ed</a> and , <a href="https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/model/USAGE">https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/model/USAGE</a>.</p><p>:string for small text.</p><p>:text for paragraph. Big text</p><p>:integer</p><p>:decimal{15,2}, (float also can be use) for decimal values, the two attributes are length and precision.</p><p>:boolean values only true and false (some databases will set this value to 0 or 1)</p><p>:date date object</p><p>:timestamp this will create the column create_at and updated_at to the database</p><p>:binary allow to store media files (images, videos, songs).</p><p><strong>Some options for columns:</strong></p><p>:uniq creates a validation for the column were every row must have unique content</p><p>:reference creates a reference with a column in a table that was already created, (belongs to)</p><p>{int} limits the amount of characters in the row</p><p>:index indexes the particular column</p><p><strong>Some options of Models generator:</strong></p><p>— parent: this will create a child relationship of the new class with parent</p><pre>rails g model Admin name:string password:digest — parent User</pre><p>This will generate:</p><pre><br>./app/models/admin.rb</pre><pre>class Admin &lt; User</pre><pre>end</pre><p>— no-test-framework it won’t create the test files if you have a test gem installed</p><h4>3. Migrations</h4><p>It will help to generate migrations to modify your existing tables:</p><p>To add a column to the User table:</p><pre><br>rails g migration AddColumnAddressToUser address:string</pre><p>This will generate the migration file with the following content:</p><pre><br>db/migrate/YYYYMMDDHHHHHHH_add_column_address_to_user.rb</pre><pre>class AddColumnAddressToUser &lt; ActiveRecord::Migration[<br> def change<br>   add_column :users, :address, :string<br> end<br>end</pre><p>To delete a column:</p><pre><br> rails g migration RemoveColumnAddressFromUser address<br></pre><p>The migration file will look something like this:</p><pre><br>db/migrate/YYYYMMDDHHHHHHH_remove_column_address_from_user.rb</pre><pre>class RemoveColumnAddressFromUser &lt; ActiveRecord::Migration<br> def change<br>   remove_column :users, :address, :string<br> end<br>end</pre><p>Rails generator gives us ways to rename columns, but the fact is the generator will create the migration files with a minimum amount of information and we need to write the migrations by hand. The syntax is:</p><pre><br>rails g migration RenameUserFirstNameToName<br></pre><p>But this is only going to generate:</p><pre><br>class RenameUserFirstNameToName &lt; ActiveRecord::Migration<br> def change</pre><pre> end<br>end</pre><p>And you will need to populate the change method by hand.</p><p>This happens in a similar fashion with changing the column type. The syntax is:</p><pre><br>rails g migration ChangeUserLastName</pre><p>And don forget to run the migrations using the command:</p><pre><br>rake db:migrate<br></pre><p><strong>4. Controllers</strong></p><p>The syntax for creating new controller is:</p><pre>rails g controller Order index show update</pre><p>This will generate a controller file, the views, tests and routes. The attributes added will populate your controller file with the actions, create routes to these attributes and create particular views for them.</p><pre><strong>create</strong>  app/controllers/order_controller.rb</pre><pre><strong>route</strong>  get &#39;order/update&#39;</pre><pre><strong>route</strong>  get &#39;order/show&#39;</pre><pre><strong>route</strong>  get &#39;order/index&#39;</pre><pre><strong>create</strong>    app/views/order</pre><pre><strong>create</strong>    app/views/order/index.html.erb</pre><pre><strong>create</strong>    app/views/order/show.html.erb</pre><pre><strong>create</strong>    app/views/order/update.html.erb</pre><p>Rails will hand roll the recently create route and you can see it in the routes file</p><pre>config/routes.rb</pre><pre>Rails.application.routes.draw do<br>  get &#39;order/index&#39;</pre><pre>  get &#39;order/show&#39;</pre><pre>  get &#39;order/update&#39;</pre><pre>end</pre><p>The controller file:</p><pre>app/controller/order_controller.rb</pre><pre>class OrderController &lt; ApplicationController<br>  def index<br>  end</pre><pre>  def show<br>  end</pre><pre>  def update<br>  end<br>end</pre><p>Rails also create the views for the controller and the attributes, this will live in the “app/views/order/” folder</p><p><strong>5. Resources</strong></p><p>The syntax for creating resources is:</p><pre>rails g resource Item name:string description:string price:float inventory:integer</pre><p>If more attributes are added, this will create model, migration, controllers, views, routes and test files, but this won’t force the creation if the files exist.</p><p>Be aware, resource generator will create more files then you may need.</p><p>These will be some of the files generated:</p><pre><strong>create</strong>    db/migrate/20181107175930_create_items.rb</pre><pre><strong>create</strong>    app/models/item.rb</pre><pre><strong>create</strong>    app/controllers/items_controller.rb</pre><pre><strong>create</strong>    app/views/items</pre><pre><strong>route</strong>    resources :items</pre><p>In the case that the resource didn’t exist, you will get:</p><p>Model</p><pre>class Item &lt; ApplicationRecord<br>end</pre><p>Migration</p><pre>class CreateItems &lt; ActiveRecord::Migration<br>  def change<br>    create_table :items do |t|<br>      t.string :name<br>      t.string :description<br>      t.float :price<br>      t.integer :inventory</pre><pre>t.timestamps<br>    end<br>  end<br>end</pre><p>Controller</p><pre>class ItemsController &lt; ApplicationController<br>end</pre><p>Routes</p><pre>Rails.application.routes.draw do<br>  resources :items<br>end</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c42daaa4b75" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>