Skip to content

Instantly share code, notes, and snippets.

@laurenhavertz
Last active December 20, 2015 11:59
Show Gist options
  • Save laurenhavertz/6127725 to your computer and use it in GitHub Desktop.
Save laurenhavertz/6127725 to your computer and use it in GitHub Desktop.
DOCUMENTATION

Things to consider

  • what do you want to build?
  • what do you want your project to have? ie. do you want your app to save data? are you using an APi?
  1. Create a visual model and relationships between them
  2. Create views (what pages do you want to see? what do they contain?)
  3. In the terminal rails new rtodo
  4. Move into new "Rtodo" folder cd rtodo
  5. open directory in sublime subl .
  6. Create User model rails g model user MAKE SURE THIS IS SINGULAR
  7. create List model rails g model list
  8. create Item model rails g model item
  9. navigate to the MODEL folder
  10. open user.rb
  • what are the attributes of the model user?
    • name
    • email
    • attr_accessible :name, :email
    • what are the relationships?
      • has_many :lists
  1. Look at the migration file that was created for the USER
  • find in DB/migrate...create_users.rb
  1. Tell the migration the attributes that we want to save
  2. each entry gets a type
  • t.string :name
  • t.string :email
  1. go back to App/Models/List
  • What are the attributes of the list?
  • attr_accessible :name, :description
  • What are the relationships?
    • LIST belongs_to :user
    • LIST has_and_belongs_to_many :items
  1. Go to the migration file DB/migrate...._create_lists.rb
  • t.string :name
  • t.string :description
  • t.integer :user_id **make this part of the migration file. Each list will belong to a specific user. Because we specify in the model that LIST belongs to USER we don't need to add.
  1. go to Model/Item *create the accessible attributes
    • attr_accessible :name
  • create the relationships
    • has_and_belongs_to_many :lists **the item may belong to many lists and the lists will have many items. by building this relationship you create the realtionships
  1. In DB/migrate...create_item.rb
  • t.string :name
  1. IN THE TERMINAL rake db:migrate
  2. create the join table rails g migration create_lists_items_table
  3. go to DB/migrate => erase def up && def down
  4. create a new change method in the new join table def change create_table :items_lists, :id => false do |t| t.integer :list_id t.integer :item_id end end
  5. rake db:migrate
  6. VERIFY THE MODELS
  • In the terminal rails c
  • User.all Item.all List.all they should all return => []
  • u = User.new Now we have a new user
  • u.name = "ryan" It should return `=> "ryan"
  • u.email = "[email protected]" => [email protected]
  • u checks the state of the object
  • u.save will show you what it did in the database (translate data into SQL) upon creation the database will create a userID
  • quit
  • rails c
  • user.all => should return all of the user data previously created
  • l = List.new
  • l.name = "shopping"
  • l.description = "my shopping list"
  • l
  • u = User.first
  • u.list
  • u.lists << l adding the list to the user
  1. TIME TO SEED THE DATABASE

  2. go to DB/migrate/seeds.rb

         User.delete_all
         List.delete_all
         Item.delete_all
         
         u = User.create(:name => 'Ryan', :email => '[email protected]')
         
         l1 = List.create(:name => 'shopping', :description => 'my shopping list')
         l2 = List.create(:name => 'house', :description => 'house chores')
         
         i1 = Item.create(:name => 'get milk')
         i2 = Item.create(:name => 'get beer')
         
         u.lists << l1
         u.lists << l2
         
         l1.items << i1
         l1.items << i2
    
  3. Add validations to our models

  4. In the user model validates_presence_of: email, name checks to make sure that your data is correct

  5. also, validates_uniqueness_of: email checks to make sure you can't use the same email twice

  6. In the list model validate_presence_of: name

  7. In the item model validates_presence_of: name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment