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.
chayok2
ReplyDelete