public
Last active

  • Download Gist
console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1.9.3-p0-falcon :001 > u = User.create!
(0.1ms) begin transaction
SQL (5.3ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Thu, 02 Feb 2012 22:28:00 UTC +00:00], ["updated_at", Thu, 02 Feb 2012 22:28:00 UTC +00:00]]
(2.0ms) commit transaction
=> #<User id: 1, created_at: "2012-02-02 22:28:00", updated_at: "2012-02-02 22:28:00">
1.9.3-p0-falcon :002 > u.steam_id = "omg"
SteamUser Load (0.2ms) SELECT "steam_users".* FROM "steam_users" WHERE "steam_users"."user_id" = 1 LIMIT 1
(0.2ms) begin transaction
SQL (0.7ms) INSERT INTO "steam_users" ("created_at", "steam_id", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", Thu, 02 Feb 2012 22:28:06 UTC +00:00], ["steam_id", nil], ["updated_at", Thu, 02 Feb 2012 22:28:06 UTC +00:00], ["user_id", 1]]
(2.4ms) commit transaction
(0.1ms) begin transaction
(0.5ms) UPDATE "steam_users" SET "steam_id" = 'omg', "updated_at" = '2012-02-02 22:28:06.419089' WHERE "steam_users"."id" = 1
(10.0ms) commit transaction
(0.1ms) begin transaction
(0.1ms) commit transaction
=> "omg"
1.9.3-p0-falcon :003 > u.steam_user
=> #<SteamUser id: 1, user_id: 1, steam_id: "omg", created_at: "2012-02-02 22:28:06", updated_at: "2012-02-02 22:28:06">
1.9.3-p0-falcon :004 >
steam_user.rb
Ruby
1 2 3
class SteamUser < ActiveRecord::Base
belongs_to :user
end
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10
class User < ActiveRecord::Base
has_one :steam_user
 
def steam_id=(value)
self.steam_user ||= SteamUser.new(:user_id => id)
steam_user.steam_id = value
steam_user.save
save
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.