Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 987
jondavidjohn Avatar asked Jan 01 '26 00:01

jondavidjohn


1 Answers

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.

like image 124
ryantm Avatar answered Jan 02 '26 13:01

ryantm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!