Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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/ruby/24.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 如何从给定字符串创建关联数组?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何从给定字符串创建关联数组?

Ruby on rails 如何从给定字符串创建关联数组?,ruby-on-rails,ruby,Ruby On Rails,Ruby,假设我有一个AR对象(object)和一个字符串(s)s基于对象的关联。即:产品属于走道,走道属于部门,部门属于商店 我想得到所有这些关联的数组。这就是我想到的 object = Product.last s='aisle.department.shop' new_array = s.split(',').each_with_object([]).with_index do |(assoc_set, array), index| a = assoc_set.split('.') a.e

假设我有一个AR对象(
object
)和一个字符串(
s
)<代码>s基于对象的关联。即:产品属于走道,走道属于部门,部门属于商店

我想得到所有这些关联的数组。这就是我想到的

object = Product.last
s='aisle.department.shop'

new_array = s.split(',').each_with_object([]).with_index do |(assoc_set, array), index|
  a = assoc_set.split('.')
  a.each_with_index do |assoc, index|
    if index == 0
      array << object.send(assoc)

    elsif index == 1
      array << object.send(a[index - 1]).send(a[index])

    elsif index == 2
      array << object.send(a[index - 2]).send(a[index - 1]).send(a[index])

    elsif index == 3
      array << object.send(a[index - 3]).send(a[index - 2]).send(a[index - 1]).send(a[index])
    end
  end
end
object=Product.last
s='过道.部门.商店'
新的_数组=s.split(',')。每个_带有_对象([])。带有_索引do |(assoc_集合,数组),索引|
a=关联集合分割('.'))
a、 每个带有索引do的|U关联,索引|
如果索引==0

数组这应该可以正常工作

object = Product.last
relations_chain='aisle.department.shop'

relations_chain.split('.').each_with_object([]).with_index do |(relation_name, array), index|
  prev_object = index.zero? ? object : array[index - 1]
  array << prev_object.send(relation_name)
end

这应该是你想要的

object = Product.last
relations_chain='aisle.department.shop'

relations_chain.split('.').each_with_object([]).with_index do |(relation_name, array), index|
  prev_object = index.zero? ? object : array[index - 1]
  array << prev_object.send(relation_name)
end

为什么需要这个字符串
过道.部门.商店
?一直都是一样的吗?你可以一个接一个地打电话给协会。结帐以获得有关如何制作的帮助dynamic@Yakov它将基于
参数
。这意味着它不会一直是相同的。为什么您需要这个字符串
过道.department.shop
?一直都是一样的吗?你可以一个接一个地打电话给协会。结帐以获得有关如何制作的帮助dynamic@Yakov它将基于
参数
。这意味着它不会一直都是一样的
class Department < ApplicationRecord
  delegate :shop, to: :department, allow_nil: true
end

class Product < ApplicationRecord
  delegate :department, to: :aisle, allow_nil: true
  delegate :shop, to: :aisle, allow_nil: true
end

relations_chain.split('.').map { |relation_name| object.send(relation_name) }