简体   繁体   中英

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 %>

I get this...

#<ActiveRecord::Relation:0x1042b2960>

obviously I expect to get hello?

Are you sure @question is a Question? The to_s response you are getting makes me think otherwise.

#<ActiveRecord::Relation:0x1042b2960>

It looks like you have a ActiveRecord::Relation, not a Question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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