我有两个表(节点和代理)。 nodes
属于agents
。我有一个将Rails项目拉入其中的脚本,我试图从ActiveRecord中提取值。我假设我要问的是它应该在控制器中查看还是在cli脚本中查看 - 或者 - 。所以,我的剧本看起来如此:
#!/usr/bin/env ruby
require '/Users/hseritt/devel/rails_projects/monitor_app/config/environment'
banner = "Banner running..."
script_dir = '/devel/rails_projects/monitor_app/monitor/bin'
class Runner
attr_accessor :banner, :agents, :agent_module, :nodes
def initialize(banner, script_dir)
@banner = banner
@agents = Agent.all
@nodes = Node.all
@script_dir = script_dir
end
def run
puts @banner
@agents.each do |agent|
if agent.active?
agent_module = '%s/%s' % [@script_dir, agent.name]
require agent_module
@nodes.each do |node|
if node.agent == agent
puts node.name
end
end
#
# HERE IS THE ISSUE:
# ns = Node.find_by_agent_id(agent.id)
# ns.each do |node|
# puts node.name
# end
#
# yields this error:
#`method_missing': undefined method `each' for #<Node:0x007fe4dc4beba0> (NoMethodError)
# I would think `ns` here would be itterable but it doesn't seem that way.
end
end
end
end
if $0 == __FILE__
runner = Runner.new(banner, script_dir)
runner.run
end
所以,这是在run方法中。没有注释掉的块当然这不是一个好的解决方案,因为每次遍历代理时,您每次都必须遍历节点。被注释掉的块对我来说似乎合乎逻辑但却引发了错误。我觉得我可能在谷歌搜索正确的东西。我在这里缺少什么?
答案 0 :(得分:0)
Node.find_all_by_agent_id
如果你不使用“all”,它只需要第一个元素