新手问题 model 中处理无数据库对应关系的属性问题 [已解决]

hellomac · 2013年07月16日 · 最后由 mingyuan0715 回复于 2013年07月16日 · 3745 次阅读

controller:

def get_host_status_api
  @server=Server.find_by_openid(session[:openid])
  @hosts = @server.hosts
  @hosts.each  do |host| 
      if  @server.skip_check
        host[running]='unknown'
      else
        get_host_status(host.ip).each_pair {|name, val| host[name]=val}
      end
  end
  respond_to do |format|
  format.json { render :json => @hosts }
  end
end

get_host_status 返回第三方接口的数据,形如{"cpu"=>"2%","mem"=>"5%"} model:

class Host < ActiveRecord::Base
  attr_accessible :ip, :nick, :server_id
  validates :ip,
            :length => {:minimum=>7,:maximum=>15},
            :uniqueness => true,
            :format => {:with=> /\d+\.\d+\.\d+\.\d+/ }
  belongs_to :server
end

看起来返回是正确的:

[{"cpu":"0.2%","created_at":"2013-07-05T17:51:53+08:00","id":126,"ip":"192.168.1.101","mem":"25%","nick":"debian\n","running":"idle","server_id":5,"updated_at":"2013-07-05T17:51:53+08:00","uptime":"1 days, 2:21","version":"20130612"},{"created_at":"2013-07-12T10:57:17+08:00","id":127,"ip":"8.8.8.8","nick":null,"running":"unknown","server_id":5,"updated_at":"2013-07-12T10:57:17+08:00"}]

但日志报错:

DEPRECATION WARNING: You're trying to create an attribute `running'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `version'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `cpu'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `mem'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `uptime'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)

按照错误信息我尝试修改 controller:

  def get_host_status_api
    @server=Server.find_by_openid(session[:openid])
    @hosts = @server.hosts
    @hosts.each  do |host| 
        if  @server.skip_check
==>        host.running='unknown'
        else
==>       get_host_status(host.ip).each_pair {|name, val| host.send("#{name}=",val)}
        end
    end
    respond_to do |format|
    format.json { render :json => @hosts }
    end
  end

model:

class Host < ActiveRecord::Base
  attr_accessible :ip, :nick, :server_id
==>  attr_accessor :running, :version, :cpu, :mem, :uptime
  validates :ip,
            :length => {:minimum=>7,:maximum=>15},
            :uniqueness => true,
            :format => {:with=> /\d+\.\d+\.\d+\.\d+/ }
  belongs_to :server
end

报错信息是没了,但返回的结果为:

[{"created_at":"2013-07-05T17:51:53+08:00","id":126,"ip":"192.168.1.101","nick":"debian\n","server_id":5,"updated_at":"2013-07-05T17:51:53+08:00"},{"created_at":"2013-07-12T10:57:17+08:00","id":127,"ip":"8.8.8.8","nick":null,"server_id":5,"updated_at":"2013-07-12T10:57:17+08:00"}]

这种情况到底应该怎么处理呢???

format.json { render :json => @hosts,:methods => [:running, :version, :cpu, :mem, :uptime] }

#1 楼 @santochancf 返回:

[{"cpu":"0.2%","created_at":"2013-07-05T17:51:53+08:00","id":126,"ip":"192.168.1.101","mem":"25%","nick":"debian\n","running":"idle","server_id":5,"updated_at":"2013-07-05T17:51:53+08:00","uptime":"1 days, 3:2","version":"20130612","running":"idle","version":"20130612","cpu":"0.2%","mem":"25%","uptime":"1 days, 3:2"},{"created_at":"2013-07-12T10:57:17+08:00","id":127,"ip":"8.8.8.8","nick":null,"running":"unknown","server_id":5,"updated_at":"2013-07-12T10:57:17+08:00","running":"unknown"}]

报错依旧 补充下: Ruby On Rails 版本为 3.2.13

#2 楼 @hellomac 还是之前的警告么?

#3 楼 @santochancf 是的。您的意思是直接在原文件上修改吧?

是的,直接改 get_host_status_api 方法里的 render 参数,attr_accessor 也要在 model 里

#5 楼 @santochancf 改完后 controller 是这样的:

def get_host_status_api
    @server=Server.find_by_openid(session[:openid])
    @hosts = @server.hosts
    @hosts.each  do |host| 
        if  @server.skip_check
==>        host.running='unknown'
        else
==>        get_host_status(host.ip).each_pair {|name, val| host.send("#{name}=",val)}
        end
    end
    respond_to do |format|
==>  format.json { render :json => @hosts,:methods => [:running, :version, :cpu, :mem, :uptime] }    
    end
  end

现在工作正常,也没有警告了. 您能稍微解释下么,或者有什么资料可以参考,我没太明白,谢谢。

@hellomac 警告是因为 rails3.2.8 以后不在自动支持 attr_writer,简单说就是不会动态创建属性的写方法, 也就是你这句:

host.send("#{name}=",val)

通过增加 attr_accessor 提供实例变量的读写方法即可,这是 Ruby 的语法,可参考 Ruby 类定义相关资料。 你设置后,返回 JSON 串没有自动 parse 你新增的实例变量读写方法,需要为 render 方法的 methods 参数来实现:

render :json => @hosts,:methods => [:running, :version, :cpu, :mem, :uptime]

这个参数可以从 Rails 的文档查到。

#7 楼 @santochancf 我之前搜到了http://stackoverflow.com/questions/10596073/deprecation-warning-for-creating-attribute-currency 但是仍然没有搞清楚,感谢您的讲解,让我对 Ruby On Rails 又多了解一点. 😄

#9 楼 @lifuzho Hi,这个帖子我搜到了,就是没看懂. 不管怎么说,非常感谢。

我是进来看头像的

我是进来看头像的

同学们镇静,要和美女聊天去豆瓣有的是。技术社区以后女生只会越来越多。

这年头能用登记照做头像的美女妹子~~ 太珍贵稀缺啊。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号