Guest User

Untitled

a guest
Feb 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. irb(main):001:0> a = Article.first
  2. ~ 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
  3. => #<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>
  4. irb(main):002:0> a.update_rating(1, 0)
  5. ~ 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"
  6. => true
  7. irb(main):003:0> a.update_rating(1, 0)
  8. ~ UPDATE "articles" SET "updated_at" = '2008-11-09T16:10:58+02:00', "vote_count" = 1, "yes_count" = 1 WHERE ("id" = 1)
  9. => true
  10.  
  11. class Article
  12. include DataMapper::Resource
  13. include Paperclip::Resource
  14. include DataMapper::Validate
  15. include DataMapper::Timestamp
  16.  
  17. property :id, Integer, :serial => true
  18. property :user_id, Integer
  19. property :category_id, Integer
  20. property :name, String, :nullable => false, :length => 200
  21. property :content, DM::Text
  22. property :counter, Integer, :default => 0
  23. property :yes_count, Integer, :default => 0
  24. property :no_count, Integer, :default => 0
  25. property :vote_count, Integer, :default => 0
  26. property :created_at, DateTime
  27. property :updated_at, DateTime
  28.  
  29. belongs_to :category
  30. has n, :tag
  31. has n, :comments
  32. has n, :article_comments
  33. belongs_to :user
  34.  
  35. validates_present :name
  36. validates_present :category_id
  37. validates_present :user_id
  38. validates_present :content
  39.  
  40. has_attached_file :avatar,
  41. :styles => { :small => "60x60#", :medium => "100x100>", :large => "200x200>" },
  42. :whiny_thumbnails => true
  43.  
  44. validates_attachment_thumbnails :avatar
  45.  
  46. def update_rating(yes, no)
  47. self.yes_count += yes
  48. self.no_count += no
  49. self.vote_count = self.no_count + self.yes_count
  50. self.dirty?
  51. end
  52.  
  53. def update_counter
  54. self.counter += 1
  55. self.save
  56. end
  57.  
  58.  
  59. end
Add Comment
Please, Sign In to add comment