Ben Kilah
Ben Kilah

Reputation: 3475

Default model field Rails 3.1

I've come from Django and was wondering how do I specify the default value of model if referenced just by calling it. Ie. <= @user %>

In Django, we can use in the model class;

def __unicode__(self):
    return self.fieldname

And it will use whatever field name or combined string we specify. Is this possible in rails?

Cheers, Ben

Upvotes: 0

Views: 90

Answers (2)

Gopal
Gopal

Reputation: 11

You can return the field you want, by implementing to_s method.

Upvotes: 1

Dylan Markow
Dylan Markow

Reputation: 124459

You probably want to define to_s in your model:

def to_s
  fieldname
end

Then in a view, you can do <%= @user %> and it'll output the fieldname you specify above.

Upvotes: 3

Related Questions