zhu2688
7/22/2016 - 9:21 AM

Trouble setting default values using a method called by the before_create hook...

Trouble setting default values using a method called by the before_create hook...

ruby-1.9.2-p180 :019 > game = Game.new
+----+------+--------+------------+------------+
| id | text | length | created_at | updated_at |
+----+------+--------+------------+------------+
|    |      |        |            |            |
+----+------+--------+------------+------------+
1 row in set
ruby-1.9.2-p180 :020 > game.text = "ha ha i am using the stack overflows!"
 => "ha ha i am using the stack overflows!" 
ruby-1.9.2-p180 :021 > game
+----+---------------------------------------+--------+------------+------------+
| id | text                                  | length | created_at | updated_at |
+----+---------------------------------------+--------+------------+------------+
|    | ha ha i am using the stack overflows! |        |            |            |
+----+---------------------------------------+--------+------------+------------+
1 row in set
ruby-1.9.2-p180 :022 > game.save
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "games" ("created_at", "length", "text", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Fri, 25 May 2012 13:58:01 UTC +00:00], ["length", 54], ["text", ""], ["updated_at", Fri, 25 May 2012 13:58:01 UTC +00:00]]
   (148.2ms)  commit transaction
 => true 
ruby-1.9.2-p180 :023 > game
+----+------+--------+-------------------------+-------------------------+
| id | text | length | created_at              | updated_at              |
+----+------+--------+-------------------------+-------------------------+
| 7  |      | 54     | 2012-05-25 13:58:01 UTC | 2012-05-25 13:58:01 UTC |
+----+------+--------+-------------------------+-------------------------+
1 row in set
ruby-1.9.2-p180 :024 > game.text = "what the...?"
 => "what the...?" 
ruby-1.9.2-p180 :025 > game.save
   (0.1ms)  begin transaction
   (0.4ms)  UPDATE "games" SET "text" = 'what the...?', "updated_at" = '2012-05-25 13:58:15.845953' WHERE "games"."id" = 7
   (186.1ms)  commit transaction
 => true 
ruby-1.9.2-p180 :026 > game
+----+--------------+--------+-------------------------+-------------------------+
| id | text         | length | created_at              | updated_at              |
+----+--------------+--------+-------------------------+-------------------------+
| 7  | what the...? | 54     | 2012-05-25 13:58:01 UTC | 2012-05-25 13:58:15 UTC |
+----+--------------+--------+-------------------------+-------------------------+
1 row in set

class Game < ActiveRecord::Base

  before_create :init
  attr_accessible :length, :text


  def init
    self.text = ""
    self.length = Random.rand(99) + 1
  end

  def finished?
    self.current_word_count == self.length
  end
  
  def unfinished?
    self.current_word_count < self.length
  end

  def self.finished
  end
  
  def self.unfinished
    t = Game.all #TODO Fix this
    t.
  end

  def self.top_current_games
    Game.unfinished[0..4]
  end  

  def current_word_count
    self.text.split(" ").length
  end

  def remaining_words
    self.length - self.current_word_count
  end

end