views:

447

answers:

2

I would like to model a betting system relationship using the power of Rails. So let's start with doing something very simple modelling the relationship from a user to a bet. I would like to have a model Bet with two primary keys.

Here are my migrations:

class CreateBets < ActiveRecord::Migration
  def self.up
    create_table :bets do |t|
      t.integer :user_1_id
      t.integer :user_2_id
      t.integer :amount

      t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end

The models:

class Bet < ActiveRecord::Base
  belongs_to :user_1,:class_name=>:User
  belongs_to :user_2,:class_name=>:User

end

class User < ActiveRecord::Base
  has_many :bets, :foreign_key  =>:user_1
  has_many :bets, :foreign_key  =>:user_2
end

when i test here in the console my relationships I got an error

>> u1=User.create :name=>"aa"
=> #<User id: 3, name: "aa", created_at: "2010-03-29 05:35:21", updated_at: "2010-03-29 05:35:21">
>> u2=User.create :name=>"bb"
=> #<User id: 4, name: "bb", created_at: "2010-03-29 05:35:29", updated_at: "2010-03-29 05:35:29">
>> b=Bet.create(:user_1=>u1,:user_2=>u2)
**************error************
TypeError: can't convert Symbol into String
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2049:in `class_eval'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2049:in `compute_type'
from /home/fenec/sources/BetTest/vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2047:in `compute_type'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/reflection.rb:151:in `send'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/reflection.rb:151:in `klass'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:254:in `raise_on_type_mismatch'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb:22:in `replace'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/associations.rb:1276:in `user_1='
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2589:in `send'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2589:in `attributes='
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2585:in `each'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2585:in `attributes='
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:2285:in `initialize'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:691:in `new'
from /home/fenec/sources/BetTest/vendor/rails/activerecord/lib/active_record/base.rb:691:in `create'

QUESTIONS:

  1. How do I define the relationships between these tables correctly?
  2. Are there any conventions to name the attributes (ex:user_1_id...)?

Thank you for your help.

+2  A: 

Here is the problem:

class Bet < ActiveRecord::Base 
  belongs_to :user_1, :class_name=> "User", :foreign_key => 'user_1_id'
  belongs_to :user_2, :class_name=> "User", :foreign_key => 'user_2_id'
end

You didn't set up your foreign_keys. They are not user_1, user_2, but user_1_id, user_1_id - just the same as in migration

for User model:

class User < ActiveRecord::Base 
  def bets
    Bet.all :conditions => ['user_1_id = ? or user_2_id = ?', self.id, self.id]
  end
end

or you can use with_scope

fl00r
even if i use the syntax you have posted , it doesnt work!
fenec
try b = Bet.new; b.user_1 = u1; b.user_2 = u2;
fl00r
It still raising an error:>> b.user_1=u1TypeError: can't convert Symbol into String
fenec
OMG! :class_name => "User" (not :User! copypaste on me).
fl00r
it works perfectly,however i still have problems retrieving all the bets that a user has done ex:>> u1.bets<br>NoMethodError: You have a nil object when you didn't expect it!You might have expected an instance of Array.
fenec
I've edited my answer for User model
fl00r
+1  A: 

I'd use a join table for this. I didn't test this code or check the syntax, but it should at least point you in the right direction. You can use validations to ensure that there are at most 2 users per bet.

class CreateBets < ActiveRecord::Migration
  def self.up
    create_table :bets do |t|

      t.integer :amount
      t.timestamps
    end
  end
end

class CreateUserBets < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.integer :user_id
      t.integer :bet_id
      t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
  end
end

class User < ActiveRecord::Base
  has_many :bets, :through => :user_bets
end

class Bet < ActiveRecord::Base
  has_man :users, :through => :user_bets
end
jshen