Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 rails可以与同一模型中的模型有多个关系吗?_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails rails可以与同一模型中的模型有多个关系吗?

Ruby on rails rails可以与同一模型中的模型有多个关系吗?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我有一个类似的\u商户模型,我存储与其他商户类似的商户 以下是表中的字段: merchant_id related_merchant_id 在这种情况下,如何编写关系? 以下是我现在拥有的: class SimilarMerchant < ActiveRecord::Base # relationships belongs_to :merchant belongs_to :merchant, :foreign_key => 'related_merchant_id'

我有一个
类似的\u商户
模型,我存储与其他商户类似的商户

以下是表中的字段:

merchant_id
related_merchant_id
在这种情况下,如何编写关系?

以下是我现在拥有的:

class SimilarMerchant < ActiveRecord::Base

  # relationships
  belongs_to :merchant
  belongs_to :merchant, :foreign_key => 'related_merchant_id'

end
class SimilarMerchant“相关商户\u id”
结束
这就足够了:

class SimilarMerchant < ActiveRecord::Base
  set_primary_key 'merchant_id'


  belongs_to :merchant, :foreign_key => 'related_merchant_id'

end
class SimilarMerchant“相关商户\u id”
结束
根据文档,外键被猜测为关联的名称加上“\u id”后缀。

这就足够了:

class SimilarMerchant < ActiveRecord::Base
  set_primary_key 'merchant_id'


  belongs_to :merchant, :foreign_key => 'related_merchant_id'

end
class SimilarMerchant“相关商户\u id”
结束

根据文档,外键被猜测为协会的名称加上一个“\u id”后缀。

但会有多个相同商户id的条目。@Jacob
商户id
是类似商户模型的主键吗?如果不是,那么为什么模型中既有
商户id
又有
相关商户id
?但是会有多个相同商户id的条目。@Jacob是
商户id
类似商户模型的主键吗?如果不是,那么为什么模型中同时存在
商户id
相关商户id
class SimilarMerchant < ActiveRecord::Base
  set_primary_key 'merchant_id'


  belongs_to :merchant, :foreign_key => 'related_merchant_id'

end