Ruby on rails 如何使用Ruby从Shopify中删除/替换智能收集规则

Ruby on rails 如何使用Ruby从Shopify中删除/替换智能收集规则,ruby-on-rails,ruby,api,collections,shopify,Ruby On Rails,Ruby,Api,Collections,Shopify,我创建了一个脚本,用于向Shopify智能集合添加条件规则,但在此之前,我需要它删除所有当前条件,以便新条件是其中唯一的条件。我遇到了一个问题,当试图删除它们时,它会返回一个未定义的错误 脚本: @update_collection = ShopifyAPI::SmartCollection.find(411011140) @a = @update_collection.rules[0].attributes @a.delete_all @update_collection.rules <

我创建了一个脚本,用于向Shopify智能集合添加条件规则,但在此之前,我需要它删除所有当前条件,以便新条件是其中唯一的条件。我遇到了一个问题,当试图删除它们时,它会返回一个未定义的错误

脚本:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete_all
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
NoMethodError: undefined method `delete_all' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cbafd008>
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete("column")
@a.delete("relation")
@a.delete("condition")
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
irb(main):1806:0> @update_collection.save
=> false
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.destroy
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

NameError: undefined local variable or method `params' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cc168280>
错误查找:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete_all
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
NoMethodError: undefined method `delete_all' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cbafd008>
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete("column")
@a.delete("relation")
@a.delete("condition")
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
irb(main):1806:0> @update_collection.save
=> false
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.destroy
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

NameError: undefined local variable or method `params' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cc168280>
irb(主):1818:0>@update\u collection.errors

“规则”=>[#]、#“标记”、“关系”=>“等于”、“条件”=>“test12”}、@prefix_options={}、@persisted=false>]}、@prefix_options={}、@persisted=true、@remote_errors=#、@validation_context=nil、@errors={:conditions=>[“无效”}、@details={:conditions=>{:conditions=>“无效”}>

我尝试了。销毁并得到以下错误:

脚本:

@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete_all
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
NoMethodError: undefined method `delete_all' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cbafd008>
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.delete("column")
@a.delete("relation")
@a.delete("condition")
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"
irb(main):1806:0> @update_collection.save
=> false
@update_collection = ShopifyAPI::SmartCollection.find(411011140)
@a = @update_collection.rules[0].attributes
@a.destroy
@update_collection.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'test12')
@update_collection.save
puts "#{update_collection.title} Updated"

NameError: undefined local variable or method `params' for #<ActiveSupport::HashWithIndifferentAccess:0x007fc2cc168280>
@update\u collection=shopifigapi::SmartCollection.find(411011140)
@a=@update\u集合。规则[0]。属性
@a、 毁灭
@更新_collection.rules'tag',:relation=>'equals',:condition=>'test12')
@更新_collection.save
将“#{update_collection.title}更新”

NameError:undefined局部变量或#

的方法'params'我只想指出,我已经为我们的问题创建了一个bandaid,尽管对于多个集合来说,它可能不是编写脚本并在有问题的集合中循环的正确方法。对于将来尝试同样事情的人,我已经加入了似乎有效的脚本

@a = ShopifyAPI::SmartCollection.find(COLLECTIONID)
# The line above finds the collection via ID
@a.rules = nil
# The line above changes all of the rules to a "null" value
@a.save
# After changing the condition rules to null we save
@b = ShopifyAPI::SmartCollection.find(COLLECTIONID)
# We go back into the collection and add a new rule (this will be the only existing rule we have now)
@b.rules << ShopifyAPI::Rule.new(:column => 'tag', :relation => 'equals', :condition => 'YOURTAG')
# The above line will add a new rule
@b.save
# We save it again to add the new rule
# If you'd like to test it you can add "@b.reload" to reload the collection information that currently exists and you should see your older rules removed and the new one in place
@a=shopifigapi::SmartCollection.find(COLLECTIONID)
#上面的行通过ID查找集合
@a、 规则=零
#上面的一行将所有规则更改为“null”值
@a、 拯救
#将条件规则更改为null后,我们保存
@b=ShopifyAPI::SmartCollection.find(COLLECTIONID)
#我们返回集合并添加一个新规则(这将是我们现在唯一的现有规则)
@b、 规则'tag',:relation=>'equals',:condition=>'YOURTAG')
#上行将添加一个新规则
@b、 拯救
#我们再次保存它以添加新规则
#如果您想对其进行测试,可以添加“@b.reload”以重新加载当前存在的集合信息,您应该会看到旧规则已删除,新规则已就位

我们不关心您的经验水平,我们只希望您进行研究,写一个清晰简洁的问题,向我们展示您的努力。记住,这不是一个“帮助我”的网站,而是一个“这里有一个问题,在未来的网站上帮助他人”,所以清晰和简洁是很重要的。“”和“”都很重要,还有“”,谢谢您对我的问题Tin Man的反馈。我没有提供足够的信息,让人觉得我希望人们“为我做这件事”,为此我道歉。我相信我在这方面做了很多研究,但仍然没有找到一种方法来完全消除这个规则。问题是我不知道如何恰当地表达我的问题,是的,我理解你并不在乎我的经验水平,这完全没问题。我的问题其实并不需要“为我做”的回答,而是指向正确的方向。我根据你的回答编辑了这个问题,并提供了更多信息