user2206724
user2206724

Reputation: 1365

Rails: Assign values to fields in model

I have two fields, first_name and brand_name. User enters first_name during sign in. And if he is an owner, he enters brand_name. brand_name by default equals first_name when the user creates an account. The user can change it later.

For this I wrote this method in the User model,

def brand_name
    first_name
end

But it sets brand_name to first_name though user enters different brand_name. I tried doing in this way:

def brand_name
    if brand_name.nil?
      first_name
    else
      brand_name
    end
end

But gives error- SystemStackError:

stack level too deep

Can anybody help in how to do this?

Upvotes: 1

Views: 929

Answers (1)

davogones
davogones

Reputation: 7399

Your method is calling itself, which leads to infinite recursion. Rewrite your method like so:

def brand_name
  self[:brand_name].present? ? self[:brand_name] : first_name
end

Upvotes: 6

Related Questions