Вопрос

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