Question

I'm working with RoR. I need to define which database field must be used as a label, just as it does CakePhp. Maybe is a basic question but I'm against the time. Thanks.

DisplayField in Cake

Was it helpful?

Solution

If I understand you correctly, you're looking to override to_s:

class User < ActiveRecord::Base
  def to_s
    username
  end
end

# usage
user = User.new(username: "jackofalltrades")
puts user # => "jackofalltrades"

If you use this technique, be sure that the attribute (username in this case) is present.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top