我有两个表(节点和代理)。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 方法中。未注释掉的块当然不是一个好的解决方案,因为每次迭代代理时,每次都必须迭代节点。被注释掉的块对我来说似乎是合乎逻辑的,但会引发错误。我在谷歌上搜索我认为正确的东西时遇到了麻烦。我在这里想念什么?