Nick
Nick

Reputation: 3090

Undefined method for Class error

In my Invitations controller I call on a method in my model:

if abc.nil?
 generate_username
end

The method is included in the Invitation model file:

def generate_username
  self.username = rand(10000000..99999999).to_s
  while Invitation.where( "lower(username) = ?", username.downcase ).first
    self.username = rand(10000000..99999999).to_s
  end
end

And in my Seeds file I have the line:

10.times do |n|
  ...
  user = User.first
  username = Invitation.generate_username
  ...
  user.active_relationships.create!( ...,
                                     username: username,
                                     ...)
end

Upon seeding I get the error below, referring to the username = line. Does anyone have an idea why this is the case and what I can do about it?

NoMethodError: undefined method `generate_username' for #<Class:0x00000009a2ab80>

Upvotes: 0

Views: 1308

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

You have an instance method, but you're trying to call it on a class. Use the method properly.

10.times do |n|
  ...
  user.generate_username
  ...
  user.active_relationships.create!( ...,
                                     username: user.username,
                                     ...)
end

Upvotes: 1

Related Questions