简体   繁体   English

覆盖rails模型中的to_s ......可能吗?

[英]Overriding to_s in rails model… possible?

I've got this model 我有这个型号

class Question < ActiveRecord::Base

    has_many :votes

    def to_s
        "hello?"
    end
end

and when I attempt to display a Question with <%= @question %> 当我尝试用<%= @question %>显示Question

I get this... 我明白了......

#<ActiveRecord::Relation:0x1042b2960>

obviously I expect to get hello? 显然我希望得到你hello?

Are you sure @question is a Question? 你确定@question是一个问题吗? The to_s response you are getting makes me think otherwise. 你得到的to_s响应让我想到了。

#<ActiveRecord::Relation:0x1042b2960>

It looks like you have a ActiveRecord::Relation, not a Question. 看起来你有一个ActiveRecord :: Relation,而不是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM