سؤال

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