Friendly_id using the value from belongs_to association

I have the following models:

class User < ActiveRecord::Base extend FriendlyId friendly_id :first_name, :use => :slugged has_one :professor after_create :create_professor def create_professor self.professor = Professor.create end end class Professor < ActiveRecord::Base extend FriendlyId friendly_id :first_name, :use => :slugged belongs_to :user def first_name user.first_name end end 

At the time of the call to create_professor and therefore, execution proceeds to the professor model ( self.professor = Professor.create ) when he tries to create slug in this method:

 def first_name user.first_name end 

I get an undefined method first_name for nil:NilClass , so it seems like the Professor object does not know what its associated user is.

Any tips on how to solve this?

+4
source share
1 answer

You do not need:

  def create_professor self.professor = Professor.create end 

Since create_professor is created by your association.

0
source

All Articles