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:
- How do I define the relationships between these tables correctly?
- Are there any conventions to name the attributes (ex:user_1_id...)?
Thank you for your help.