Linking across models

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

  • 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.

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

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

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! :-)]

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

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
}%>