Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- irb(main):001:0> a = Article.first
- ~ SELECT "id", "user_id", "category_id", "name", "counter", "yes_count", "no_count", "vote_count", "created_at", "updated_at", "avatar_file_name", "avatar_content_type", "avatar_file_size", "avatar_updated_at", "tag_id" FROM "articles" ORDER BY "id" LIMIT 1
- => #<Article id=1 user_id=1 category_id=1 name="wreerqwrwewqer" content=<not loaded> counter=0 yes_count=0 no_count=0 vote_count=0 created_at=#<DateTime: 23565888871/9600,1/12,2299161> updated_at=#<DateTime: 23565888871/9600,1/12,2299161> avatar_file_name=nil avatar_content_type=nil avatar_file_size=nil avatar_updated_at=nil tag_id=nil>
- irb(main):002:0> a.update_rating(1, 0)
- ~ SELECT "content", "id", "user_id", "category_id", "name", "counter", "yes_count", "no_count", "vote_count", "created_at", "updated_at", "avatar_file_name", "avatar_content_type", "avatar_file_size", "avatar_updated_at", "tag_id" FROM "articles" WHERE ("id" = 1) ORDER BY "id"
- => true
- irb(main):003:0> a.update_rating(1, 0)
- ~ UPDATE "articles" SET "updated_at" = '2008-11-09T16:10:58+02:00', "vote_count" = 1, "yes_count" = 1 WHERE ("id" = 1)
- => true
- class Article
- include DataMapper::Resource
- include Paperclip::Resource
- include DataMapper::Validate
- include DataMapper::Timestamp
- property :id, Integer, :serial => true
- property :user_id, Integer
- property :category_id, Integer
- property :name, String, :nullable => false, :length => 200
- property :content, DM::Text
- property :counter, Integer, :default => 0
- property :yes_count, Integer, :default => 0
- property :no_count, Integer, :default => 0
- property :vote_count, Integer, :default => 0
- property :created_at, DateTime
- property :updated_at, DateTime
- belongs_to :category
- has n, :tag
- has n, :comments
- has n, :article_comments
- belongs_to :user
- validates_present :name
- validates_present :category_id
- validates_present :user_id
- validates_present :content
- has_attached_file :avatar,
- :styles => { :small => "60x60#", :medium => "100x100>", :large => "200x200>" },
- :whiny_thumbnails => true
- validates_attachment_thumbnails :avatar
- def update_rating(yes, no)
- self.yes_count += yes
- self.no_count += no
- self.vote_count = self.no_count + self.yes_count
- self.dirty?
- end
- def update_counter
- self.counter += 1
- self.save
- end
- end
Add Comment
Please, Sign In to add comment