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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top