I'm using rails ActiveModel, I defined 2 methods like this:
def find_existing_task(task)
existing_one = Task.find(task.id)
end
def find_existing_person(person)
existing_one = People.find(person.id)
end
But I think I need a more generic method like this:
def find_existing(any_active_model_instance_with_id)
existing_one = ActiveModelClass.find(any_active_model_instance_with_id.id)
end
But I don't know how to call the class method given an instance, in above, given task, I can call Task.find without specifing class name "Task"
Any solution? thanks!
You can do it with the following code
def find_existing(some_model)
existing_one = some_model.class.find(some_model.id)
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.