has_many, has_one and calling a self.method

Hi      I have the following classes and relations ServiceDeskTicket     has_many :service_desk_status_histories     has_one :service_desk_resolution

ServiceDeskResolution belongs_to :service_desk_ticket def self.add_resolution_on_convert_to_incident(sd_id,status_id,created_by_id,modified_by_id)   puts 'in resolution status is '+status_id.to_s end

ServiceDeskStatusHistory     belongs_to :service_desk_ticket      self.add_status_histories_on_convert(sd_id,status_id,created_by_id,modified_by_id)    puts 'called add_status_histories_on_convert end

Now from ServiceDeskTicket in a def I tried

sd_ticket=self.find(ticket_id) sd_ticket.service_desk_resolution.add_resolution_on_convert(sd_ticket.id,sd_ticket.service_desk_status_id,created_by,modified_by)       Then I get error error is undefined method `add_resolution_on_convert' for #<ServiceDeskResolution:0xb73e2120>

           But when I call sd_ticket.add_status_histories_on_convert(sd_id,status_id,created_by_id,modified_by_id)

       It works properly Please tell me why this happens

Thanks in advance Sijo

Well it shouldn't be a surprise that calling a class method on an instance doesn't work. That's just the way things work. The second case is the interesting one. I assume you mean

sd_ticket.service_desk_status_histories.add_status_histories_on_convert(...)

The reason this works is because sd_ticket.service_desk_status_histories is a proxy. Conceptually it is a bit like a scoped version of ServiceDeskStatusHistory (scoped so that it belongs to sd_ticket). For example sd_ticket.service_desk_status_histories.find will only find instances of ServiceDeskStatusHistory belonging to that ticket. In a similar way class methods are available on this proxy object (with_scope is used to scope them to the owner)

Fred

Hi     Thanks for your reply.Then how can I call class method of ServiceDeskResolution from ServiceDeskTicket .I also tried like

sd_resolution_obj=ServiceDeskResolution.new sd_resolution_obj.add_resolution_on_convert_to_incident(sd_ticket.id,sd_ticket.service_desk_status_id,created_by,modified_by)

And the exception caught was undefined method `add_resolution_on_convert_to_incident' for #<ServiceDeskResolution:0xb72d66f0>       Could you please tell me how can I solve this?

Sijo

Hi    Thanks for your reply.Then how can I call class method of ServiceDeskResolution from ServiceDeskTicket .I also tried like

sd_resolution_obj=ServiceDeskResolution.new sd_resolution_obj .add_resolution_on_convert_to_incident (sd_ticket.id,sd_ticket.service_desk_status_id,created_by,modified_by)

And the exception caught was undefined method `add_resolution_on_convert_to_incident' for #<ServiceDeskResolution:0xb72d66f0>      Could you please tell me how can I solve this?

It's a class method. call it on the class:

ServiceDeskResolution .add_resolution_on_convert_to_incident (sd_ticket.id,sd_ticket.service_desk_status_id,created_by,modified_by)

Fred

Hi     Thanks for your reply.Now it works..Actually before I was coding complete thing in the controller and now I am changing that .And thus tries to obey the principle of MVC (for example write the DB depenedent code to corresponding model).But unfortunately till now I have not got a good tutorial explaining how to separate all these (especially for a beginner in rails since this is my first industrial project ).I am not sure any such exists..Since this is a self learning process it may take time.. Isn't it?

Sijo