Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 添加记录关系时“TypeError:无法将符号转换为字符串”_Ruby On Rails_Ruby_Foreign Key Relationship - Fatal编程技术网

Ruby on rails 添加记录关系时“TypeError:无法将符号转换为字符串”

Ruby on rails 添加记录关系时“TypeError:无法将符号转换为字符串”,ruby-on-rails,ruby,foreign-key-relationship,Ruby On Rails,Ruby,Foreign Key Relationship,我想建立一个从用户到下注的博彩系统关系模型。我想要一个有两个主键的模型赌注 以下是我的迁移: 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 e

我想建立一个从用户到下注的博彩系统关系模型。我想要一个有两个主键的模型赌注

以下是我的迁移:

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
模型:

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
当我在控制台中测试我的关系时,我得到了一个错误

>> 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'
问题:

如何定义关系 在这些桌子之间正确吗? 是否有命名属性的约定,例如:user\u 1\u id。。。? 谢谢您的帮助。

问题出在这里:

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
你没有设置外键。它们不是user_1,user_2,而是user_1_id,user_1_id-与迁移中的相同

对于用户模型:

class User < ActiveRecord::Base 
  def bets
    Bet.all :conditions => ['user_1_id = ? or user_2_id = ?', self.id, self.id]
  end
end
或者,您可以与\u scope一起使用以下问题:

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
你没有设置外键。它们不是user_1,user_2,而是user_1_id,user_1_id-与迁移中的相同

对于用户模型:

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

或者您可以使用with_scope

我会为此使用联接表。我没有测试这段代码或检查语法,但它至少应该为您指出正确的方向。您可以使用验证来确保每次下注最多有2个用户

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

我会使用一个联接表。我没有测试这段代码或检查语法,但它至少应该为您指出正确的方向。您可以使用验证来确保每次下注最多有2个用户

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

为什么打赌需要有两个用户连接到它?这可能也值得整理一下你的例子,它们很难阅读。这篇文章现在很干净了,在db的两个字段中存储两个用户真的很奇怪。我建议实施has_many:users解决方案,验证的用户数不超过2个,或者每个bet正好有2个用户为什么一次下注需要有两个用户?这可能也值得整理一下你的例子,它们很难阅读。这篇文章现在很干净了,在db的两个字段中存储两个用户真的很奇怪。我建议实施多用户解决方案,验证用户数量不超过2个,或者每个bettry b=Bet.new正好有2个用户;b、 用户_1=u1;b、 用户_2=u2;它仍然引发错误:>>b.user_1=u1 TypeError:无法将符号转换为StringOMG!:class_name=>用户不是:用户!copypaste在我身上。它工作得很好,但是我在检索用户所做的所有赌注时仍然有问题,例如:>>u1.bets nomethoder错误:当你没有预料到它时,你有一个nil对象!您可能希望有Array.try b=Bet.new的实例;b、 用户_1=u1;b、 用户_2=u2;它仍然引发错误:>>b.user_1=u1 TypeError:无法将符号转换为StringOMG!:class_name=>用户不是:用户!copypaste在我身上。它工作得很好,但是我在检索用户所做的所有赌注时仍然有问题,例如:>>u1.bets nomethoder错误:当你没有预料到它时,你有一个nil对象!您可能期望数组的一个实例。