- 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?
- Create a visual model and relationships between them
- Create views (what pages do you want to see? what do they contain?)
- In the terminal
rails new rtodo - Move into new "Rtodo" folder
cd rtodo - open directory in sublime
subl . - Create User model
rails g model userMAKE SURE THIS IS SINGULAR - create List model
rails g model list - create Item model
rails g model item - navigate to the MODEL folder
- open user.rb
- what are the attributes of the model user?
- name
attr_accessible :name, :email- what are the relationships?
has_many :lists
- Look at the migration file that was created for the USER
- find in DB/migrate...create_users.rb
- Tell the migration the attributes that we want to save
- each entry gets a type
t.string :namet.string :email
- 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
- LIST
- Go to the migration file
DB/migrate...._create_lists.rb
t.string :namet.string :descriptiont.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.
- 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
- In DB/migrate...create_item.rb
t.string :name
- IN THE TERMINAL
rake db:migrate - create the join table
rails g migration create_lists_items_table - go to DB/migrate => erase
def up && def down - 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
rake db:migrate- VERIFY THE MODELS
- In the terminal
rails c User.allItem.allList.allthey should all return=> []u = User.newNow we have a new useru.name = "ryan"It should return `=> "ryan"u.email = "[email protected]" => [email protected]uchecks the state of the objectu.savewill show you what it did in the database (translate data into SQL) upon creation the database will create a userIDquitrails cuser.all=> should return all of the user data previously createdl = List.newl.name = "shopping"l.description = "my shopping list"lu = User.firstu.listu.lists << ladding the list to the user
-
TIME TO SEED THE DATABASE
-
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 -
Add validations to our models
-
In the user model
validates_presence_of: email, namechecks to make sure that your data is correct -
also,
validates_uniqueness_of: emailchecks to make sure you can't use the same email twice -
In the list model
validate_presence_of: name -
In the item model
validates_presence_of: name