# Linking across models

**URL:** <https://www.ruby-forum.com/t/linking-across-models/110546>\
**Category:** Rails\
**Created:** [July 27, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546 "2007-07-27T12:41:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/1 "2007-07-27T12:41:38Z")

</div>

Bit new at this so not sure if this is something that shouldn’t be  
done but I currently have a number of models which are attached to  
database tables and deal with all the backend actions like populating  
editing and updating of the tables.

I now want to be able to create a user view to take care of the end  
user actions, searching etc , this is working fine and i have a model  
and controller sorted to do the searching, once i’ve found and  
displayed a list of the data though i want to be able to call the show  
view of the backend model is it possible to use a link\_to or something  
else to make this call from one model to another when i try to do it  
at the moment the link to is just searching within the current view  
folder

Any help on this would be great

Cheers

Matt

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/2 "2007-07-27T14:30:47Z")

</div>

- models don’t “deal with […] like populating editing and updating  
of the tables.” thats the controllers job. Controllers do actions with  
help of the model.
- link\_to dosn’t link to views, only to controllers and actions.
- You don’t need different models for backend and frontend
- you only (might) need different controllers.

for those points i’m a bit confused about what your problem exactly  
is.

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/3 "2007-07-27T14:42:48Z")

</div>

Hi Thorsten,

Thanks for the reply, Its quite possible I have too many of  
everything! very new to this and more than a little confused!

Basically I have a model covering a table holding degree programmes  
and a model covering a table holding careers information(habtm), at  
the moment they both have thier own controllers and views which are  
fairly similar to the original scaffold, is this wrong?

what i’m trying to do now is put in place a single front end page for  
the user to access a search function for both tables, at the moment  
this also has its own model called main and controller because I  
couldn’t work out where to put its view otherwise? so where I am at is  
that once i have the search results for either careers or degrees I  
want to let the user click a link to go to the show view of the  
appropriate model, but I can’t work out how to do this from the  
results.rhtml page which is held in the view for main.

Very convoluted I know, which does lead me to suspect i’m not doing it  
how its supposed to be done but any pointers or a solution would be  
grand

cheers

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/4 "2007-07-27T15:11:54Z")

</div>

OK

you have 2 tables in db (3 including your join table, since its a  
habtm relationship) their for you have 2 models 1 for each table.

next make sure that the relationships are correct in the models

class Career \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :degrees

class Degree \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :careers

Then in your controller call  
@careers = Career.find(…)  
in the appropriate view/method

because you have you table associations set up right you will be able  
to call degrees results from from your careers variable  
\<% for career in @careers %\>  
\<% for degree in career.degrees %\>  
\<%= degree %\>  
\<% end %\>  
\<% end %\>

I hope this helps but im not sure as I found your 1st and 2nd  
description of the problem a little hard to follow.

Best advice i can give you is buy “agile web development with rails”  
from pragmatic bookshelf

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/5 "2007-07-27T15:08:54Z")

</div>

On Fri, 27 Jul 2007, Matt\_99 wrote:

> Bit new at this so not sure if this is something that shouldn’t be  
> done but I currently have a number of models which are attached to  
> database tables and deal with all the backend actions like populating  
> editing and updating of the tables.

That sounds fine so far. One model (approx) per table, because  
each table row is an instance of an object.

> I now want to be able to create a user view to take care of the end  
> user actions, searching etc , this is working fine and i have a model  
> and controller sorted to do the searching, once i’ve found and

I don’t think you should need a model for the searching. Basically  
the actions provoked by the view invoke methods of the controller.  
I think it’s probably OK to have a model for the searching, if you  
prefer that, it’s just it isn’t strictly necessary to make that an  
object.

> displayed a list of the data though i want to be able to call the show  
> view of the backend model is it possible to use a link\_to or something  
> else to make this call from one model to another when i try to do it  
> at the moment the link to is just searching within the current view  
> folder

I think you are a bit mixed up about the model, view, controller  
setup. This is about separation of concerns. The models are the  
data made into objects (reified, if you like) that the controllers  
manipulate. The controllers display this as views. Thus if the  
view changes, the controller is not concerned with presentation, so  
need not be changed at the same time. If the model changes, the  
view need not necessarily (the change may only affect business  
logic). There are times when all will be affected, but in general  
they won’t.

I think the best description I’ve seen of this is in “Head First  
Design Patterns”. [Let’s avoid discussion about the stylistic  
aspects of that series! :-)]

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 27, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/6 "2007-07-27T15:35:58Z")

</div>

Cheers folks, that i think make it a bit clearer, and I think i will  
buy the book!

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [July 28, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/linking-across-models/110546/7 "2007-07-28T15:08:17Z")

</div>

For anyone else trying to do this in the end the solution was to  
specify a different controller in the link\_to call

\<%= link\_to “Show”,  
{ :controller =\> “careers”, :action =\> “show”, :id =\> career  
}%\>
