在ruby-on-rails中,能否自定义模型to_s方法?
- 内容介绍
- 相关推荐
本文共计149个文字,预计阅读时间需要1分钟。
我有这个模型,class Question ActiveRecord::Base has_many :votes
def to_s hello?
end
当我尝试用=> @question 显示问题时,我明白了...
class Question < ActiveRecord::Base has_many :votes def to_s "hello?" end end
当我尝试用<%= @question%>显示问题时
我明白了……
#< ActiveRecord的::关联:0x1042b2960>
显然我希望得到你好吗?
你确定@question是一个问题吗?你得到的to_s响应让我想到了.#<ActiveRecord::Relation:0x1042b2960>
看起来你有一个ActiveRecord :: Relation,而不是一个问题.
本文共计149个文字,预计阅读时间需要1分钟。
我有这个模型,class Question ActiveRecord::Base has_many :votes
def to_s hello?
end
当我尝试用=> @question 显示问题时,我明白了...
class Question < ActiveRecord::Base has_many :votes def to_s "hello?" end end
当我尝试用<%= @question%>显示问题时
我明白了……
#< ActiveRecord的::关联:0x1042b2960>
显然我希望得到你好吗?
你确定@question是一个问题吗?你得到的to_s响应让我想到了.#<ActiveRecord::Relation:0x1042b2960>
看起来你有一个ActiveRecord :: Relation,而不是一个问题.

