Thứ Tư, 10 tháng 9, 2014

Model - CRUD là gì trong RoRs

CRUD: Create, Read, Update, Delete. Được tích hợp sẵn trong Active Record của RoRs.
article=Article.new # Tạo mới objects article từ Class Article
article.new_record # Kiển tra objects đã lưu chưa true/false
article.attributes # Kiểm tra các thuộc tính và giá trị thuộc tính của Object article hiện hành
Ghi thông tin vào các Artributes
article.title='Title for this article'
article.body='Body content of this article'
Truy xuất để kiểm tra thông tin đã ghi
artical
Lưu thông tin
article.save
Đếm số lượng record trong model ArticleArticle.count
Tạo và lưu một Object
Cách 1:
article = Article.new(:title => "Introduction to Active Record",  
:body => "Active Record is Rails's default ORM..", :published_at => Date.today)
>> article.save
Cách 2:Article.create(:title => "RubyConf 2010", :body => "The annual RubyConf will 
take place in..", :published_at => '2010-05-19') 

Chú ý: 
article.title='something' tương đương với article.title=('something'). title=() là một phương thức

Không có nhận xét nào:

Đăng nhận xét