# Accessing one objects methods from within another object

**URL:** <https://www.ruby-forum.com/t/accessing-one-objects-methods-from-within-another-object/154420>\
**Category:** Ruby\
**Created:** [November 29, 2008, 6:44am UTC](https://www.ruby-forum.com/t/accessing-one-objects-methods-from-within-another-object/154420 "2008-11-29T06:44:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 29, 2008, 6:44am UTC](https://www.ruby-forum.com/t/accessing-one-objects-methods-from-within-another-object/154420/1 "2008-11-29T06:44:06Z")

</div>

Ive created a simple todo list app for the console and tried to  
implement mvc using my very very very limited mvc knowledge. In the app  
users can create single tasks. they can also group tasks with a similiar  
theme together into a project. I thought id learn more about mvc and oop  
than just reading or using some framework.

So i have two basic class definitions

class Project\_controller  
class Task\_controller

Ive added basic CRUD functionality to them. But now they need to  
interact with one anohter in a few situations. For example, when a user  
wants to delete a project they might also want to delete the associated  
tasks with that project. So in project\_controller.delete() I want to be  
able to access the task objects delete method. I thought about passing  
the object itself as a parameter e.g

class Project\_controller  
def delete a\_task\_controller  
#blah blah blah  
#user says yes delete the associated tasks  
a\_task\_controller.delete  
end

but that doesnt seem pratical because if i start adding other  
controllers such as say “Category” i could end up with a huge parameter  
list

I then thought about using initialize like this

class Project\_controller  
def initialize a\_task\_controller  
@a\_task\_controller = a\_task\_controller  
end  
end

but then ill also need to do the same with Task\_controller and that will  
lead to a run error due to the ole what comes first the chicken or the  
egg.

task\_cntr = Task\_controller.new (proj\_cntr) #proj\_cntr doesnt exist yet  
so boom!  
proj\_cntr = Project\_controller.new (task\_cntr) #this is ok though

Whats teh best way of doing this, is there a way i make all controller  
objects be able to access other controllers methods? Is the observer  
pattern useful here? Ive never used it but know a bit about it.

Sorry if the titles vague. Im trying to develop a simple todo list  
console app and decided to try and use mvc principles with what limited  
knowledge of mvc i have.

in the apps a user can define projects such as “Buy a new PC” and then  
assign new tasks to this project such as “Research Anandtech for best  
value parts”, “find cheapest price of parts on [froogle.com](http://froogle.com)”.

Hence i have a projects controller class and a tasks controller class.  
At the moment they are defined as seperate classes but i can see now  
that they could inheirt from an abstract parent “Controller” class but  
at the time i didnt know what the common code would be as im a newb.

Alls gone well as i created pretty much each respective items basic CRUD  
code and they didnt interact. However when it comes to deleting a  
project i realised that the user should be able to decide if they want  
to delete the tasks associated with it as well. I dont want to copy and  
paste my delete code from my tasks controller over the project  
controller as its duplication and contradicts oop.

I want to be able to do just call the task controller objects delete  
method from within the project controller objects delete method.

like

class project\_controller  
def delete  
#choose project to delete  
project\_to\_delete = users\_choice()  
#now display the associated tasks with this project  
task\_controller\_object.display\_tasks\_for\_project(project\_to\_delete)  
#find out if user wants to delete tasks as well…  
user\_wants\_to\_delete = get\_response()  
if user\_wants\_to\_delete == true  
#use the task cntr object once again to do its own dirty work  
task\_controller\_object.delete(all\_tasks\_associated\_with\_this\_project)  
…  
…  
blah blah blah  
end

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 29, 2008, 6:45am UTC](https://www.ruby-forum.com/t/accessing-one-objects-methods-from-within-another-object/154420/2 "2008-11-29T06:45:07Z")

</div>

sorry the bottom part of that mail was a previous version of my message,  
forgot to delete it

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [November 29, 2008, 8:34am UTC](https://www.ruby-forum.com/t/accessing-one-objects-methods-from-within-another-object/154420/3 "2008-11-29T08:34:19Z")

</div>

Hi –

On Sat, 29 Nov 2008, Adam A. wrote:

> #user says yes delete the associated tasks  
> def initialize a\_task\_controller  
> proj\_cntr = Project\_controller.new (task\_cntr) #this is ok though
> 
> Whats teh best way of doing this, is there a way i make all controller  
> objects be able to access other controllers methods? Is the observer  
> pattern useful here? Ive never used it but know a bit about it.

It looks like you’re trying to accomplish the M part of MVC in the C  
part. You should think about having not just controller classes, but a  
Task class and a Project class. The Task and Project objects would  
have knowledge of their relation (a task is part of a project, etc.),  
so that when you delete a project, it can delete the tasks that were  
part of it.

The controllers should just be firing off the high-level commands,  
like telling a project to die. What happens when a project dies is  
really in the purview of the Project object itself.

David
