# Has\_many, has\_one and calling a self.method

**URL:** <https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390>\
**Category:** Rails\
**Created:** [September 24, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390 "2008-09-24T12:00:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390/1 "2008-09-24T12:00:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390/2 "2008-09-24T12:08:52Z")

</div>

On 24 Sep 2008, at 11:00, Sijo Kg wrote:

> ```
> 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
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390/3 "2008-09-24T12:21:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390/4 "2008-09-24T12:38:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 24, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/has-many-has-one-and-calling-a-self-method/149390/5 "2008-09-24T12:27:07Z")

</div>

On 24 Sep 2008, at 11:21, Sijo Kg wrote:

> 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
