Darshan Patel Darshan Patel - 17 days ago 12
Ruby Question

How to call active record's association method if we have same menthod name in current model

I am new to Ruby and Rails and I did following code

class Department
end
class Employee
field :depaertment_id, :default => nil
belongs_to :department, :autosave => false
def department
dept = self.super.department # check whether associated department object exists as per self.department active record's method
if dept.nil?
#other operation
end
end
end


Here from
department method
I need get
department object


If I do following code then department object is easily available as per rails association

class Employee
field :depaertment_id, :default => nil
belongs_to :department, :autosave => false
def get_department
dept = self.department
if dept.nil?
#other operation
end
end
end


Please help me, How can i get department object
Thanks in advance.

Answer

Used Method Wrapping from Monkey patching

class Department
end
class Employee
  field :depaertment_id, :default => nil
  belongs_to :department, :autosave => false
  old_dept = instance_method(:department)
  define_method(:department) do
  dept = old_bar.bind(self).()
  if dept.nil?
      #other code...
  end
end