Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.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/5/sql/68.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
Mysql 如何查询rails方式?轨道3.2_Mysql_Sql_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Mysql 如何查询rails方式?轨道3.2

Mysql 如何查询rails方式?轨道3.2,mysql,sql,ruby,ruby-on-rails-3,activerecord,Mysql,Sql,Ruby,Ruby On Rails 3,Activerecord,模型之间的关系列表: class ErrorScope < ActiveRecord::Base belongs_to :server has_many :scope_to_fixflow_map attr_accessible :id, :server_id, :error_codes, :scoping_method, :priority, :error_codes_is_wildcard_match serialize :error_codes ..... en

模型之间的关系列表:

class ErrorScope  < ActiveRecord::Base
  belongs_to :server
  has_many :scope_to_fixflow_map
  attr_accessible :id, :server_id, :error_codes, :scoping_method, :priority, :error_codes_is_wildcard_match
  serialize :error_codes
  .....
end

class ScopeToFixflowMap < ActiveRecord::Base
  belongs_to :error_scope
  attr_accessible :id, :server_id, :error_scope_id, :path, :fixflow_class_name
  ......
end

class Server < ActiveRecord::Base
  has_many :error_scopes 
  ......
end
现在我有了一个sql查询,它为我提供了所需的输出:

SELECT fixflow_class_name 
FROM error_scopes s
join scope_to_fixflow_maps m on s.id=m.error_scope_id
join servers serv on serv.id=s.server_id
where error_codes regexp 'error_scope_test'
and path = 'x'
and assettag = 'y'
到目前为止我都试过了。它起作用了

ErrorScope.where("error_codes like ?", "%error_scope_test\n%").select {|tag| tag.server.assettag == "y"}[0].scope_to_fixflow_map.select {|y| y.path == "x"}[0].fixflow_class_name
使用连接

ErrorScope.joins(:server, :scope_to_fixflow_map).where("error_codes LIKE ?", "%error_scope_test%").select {|tag| tag.server.assettag == "y"}[0].scope_to_fixflow_map.select {|y| y.path == "x"}[0].fixflow_class_name

我相信一定有更好的方法来做这个查询

不是轨道,而是又快又脏:

ActiveRecord::Base.execute("SELECT fixflow_class_name 
FROM error_scopes s
join scope_to_fixflow_maps m on s.id=m.error_scope_id
join servers serv on serv.id=s.server_id
where error_codes regexp 'error_scope_test'
and path = 'x'
and assettag = 'y'")
返回可以使用的哈希数组,如下所示:

ErrorScope.joins(:server, :scope_to_fixflow_map)
.where("error_codes LIKE ?", "%error_scope_test%") 
.where("servers.assettag='y'")
.where("scope_to_fixflow_maps.path='x'")
.select("scope_to_fixflow_maps.fixflow_class_name")

从SQL中不清楚您引用的各个列在哪些表上。ErrorScope.joins:server,:scope\u to\u fixflow\u map.whereerror\u代码如?,%error\u scope\u test%这很好。当我与此链接时,whereserver.assettag='y'会给我错误信息。ActiveRecord::StatementInvalid:Mysql2::Error:where子句中的未知列“server.assettag”:@Kavincat Yeah,应该是服务器和范围到固定流的映射,而不是服务器和范围到固定流的映射。更新了答案。
ErrorScope.joins(:server, :scope_to_fixflow_map)
.where("error_codes LIKE ?", "%error_scope_test%") 
.where("servers.assettag='y'")
.where("scope_to_fixflow_maps.path='x'")
.select("scope_to_fixflow_maps.fixflow_class_name")