Thursday, December 2, 2010

Creating the malaysian facebook ;) [Rails 3 Relational database example]

Scaffolding the structure of the application
first generate the scaffolding.(bloody easy on Ruby on Rails)
rails g scaffold user name:text address:text
rails g scaffold post msg:text user_id:text
rails g scaffold comment msg:text user_id:text post_id:text
 
Creating a relational database
Second is the model configuration

#inside akob/app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end

#inside akob/app/models/post.rb
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
end

#inside akob/app/models/user.rb
class User < ActiveRecord::Base
has_many :posts
has_many :comments, :through => :posts
validates :name, :presence => true
end

this is just an example, we should put many MANY validation to assure this system reliability. inside this example it shows how the data is relate to each other.

this is the screenshot of the console testing which shows the data relationship is working fine. ;)
Actually this is part 1. In the next post, I will move onto the view files & show to you guys how it works with ajax.
Would like to here some respond from you ;)

1 comment: