- 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 user
MAKE 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 :name
t.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 :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.
- 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.all
Item.all
List.all
they should all return=> []
u = User.new
Now we have a new useru.name = "ryan"
It should return `=> "ryan"u.email = "[email protected]" => [email protected]
u
checks the state of the objectu.save
will show you what it did in the database (translate data into SQL) upon creation the database will create a userIDquit
rails c
user.all
=> should return all of the user data previously createdl = 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
-
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, name
checks to make sure that your data is correct -
also,
validates_uniqueness_of: email
checks 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