Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 4 有许多不同名称的多个关联_Ruby On Rails 4_Associations_Rails Activerecord_Has Many - Fatal编程技术网

Ruby on rails 4 有许多不同名称的多个关联

Ruby on rails 4 有许多不同名称的多个关联,ruby-on-rails-4,associations,rails-activerecord,has-many,Ruby On Rails 4,Associations,Rails Activerecord,Has Many,Ruby 2.1.0/Rails 4.0.2 我有一个总线模型和一个集群模型。它们如下所示: class Bus < ActiveRecord::Base has_many :left_centroids, class_name: "Centroid" has_many :right_centroids, class_name: "Centroid" end class Centroid < ActiveRecord::Base belongs_to :bus end

Ruby 2.1.0/Rails 4.0.2

我有一个总线模型和一个集群模型。它们如下所示:

class Bus < ActiveRecord::Base
  has_many :left_centroids, class_name: "Centroid"
  has_many :right_centroids, class_name: "Centroid"
end

class Centroid < ActiveRecord::Base
  belongs_to :bus
end

能否在质心表中添加另一列,称为“类型”。现在,您可以使用它对关联应用条件。像这样

 class Bus < ActiveRecord::Base
   has_many :left_centroids, class_name: "Centroid", -> { where type: 'left_centroid' }
   has_many :right_centroids, class_name: "Centroid", -> { where type: 'right_centroid' }
 end
类总线{where type:'left\u Centroid'} 有很多:右质心,类名:“质心”,->{where type:'right\u Centroid'} 结束
不确定语法,但我认为这应该有帮助。

你能发布你说的算法吗?我使用的是ai4r gem,算法的详细信息可以在这里找到。然而,我以前尝试过这个算法,这是我第一次遇到这个问题。我将为集群算法更新一些自定义代码(很可能是问题的根源):我可以,但质心已经有一个名为“direction”的列。我会试试的。仅使用关联中的条件。让我们知道它是否有用。奇怪的是,它不起作用。不过,我设法避免使用相同的模型声明两个字段。谢谢你的帮助。
 class Bus < ActiveRecord::Base
   has_many :left_centroids, class_name: "Centroid", -> { where type: 'left_centroid' }
   has_many :right_centroids, class_name: "Centroid", -> { where type: 'right_centroid' }
 end