質問

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