Topic: implementing has_many relationships

Hi all,
I'm new to ruby and rails and I like it so far smile  I have what seems to be a simple question, so here goes.

I've followed this tutorial (part 1 and part 2):
http://fairleads.blogspot.com/2007/12/r … -step.html

Now, I'm trying to understand how to implement a has_many relationship with some new data.  For example, say I want to add Actors to this application using a "Movie has_many Actors" relationship.  For simplicity, let's assume an actor can only be in one movie.  How should I begin implementing this?  I know I'd have to put "has_many :actors" in the Movie model, and "belongs_to :movie" in the Actor model.  But what else is needed to get all the "plumbing" in place.  Any tips or help is greatly appreciated.

Thanks!

Dave Kroondyk

Re: implementing has_many relationships

Hi davekaro,

That's pretty much all you need. Just make sure that you have a field to store the foreign key in the table on the belongs_to side of the association, and you are done like dinner. In this case, with the rails defaults, you would need a movie_id field in the actors table. Rails magically takes care of the rest.

Re: implementing has_many relationships

Thanks for the reply.  So, I'm still not quite sure where the code would go that adds an actor to a movie.  Can it be in the same controller (movie controller)?  If so, what would the code look like?  Again, I really appreciate any info on this.  I feel stupid for not being able to grasp this, but I think once I do, I'll be able to understand more and more about ruby and rails.

Dave

Re: implementing has_many relationships

As a quick follow up, I revisted Akita's tutorial ( http://www.akitaonrails.com/2007/12/12/ … l-tutorial ) and was able to implement the has_many relationship successfully.  However, Akita's tutorial seems to get pretty involved with routes which made implementing the has_many relationship seem like a non-trivial task (i.e. several times the tutorial had me change text from what the scaffold generated - this was not something I think I could have solved on my own with my limited rails experience).  I'll have to do some reading on routes and get used to them, but Akita's tutorial was pretty simple to follow.

Dave

Re: implementing has_many relationships

Oh, sorry, I didn't understand you completely before. Don't feel stupid; it takes a while to really get the rails way, but then you fly like you've never flow before!

OK, so there are no hard-and-fast rules about where it should go, but definitely in a controller. You could do something like this if you wanted to put it in the MoviesController:

class MoviesController < ApplicationController
  # the rest of your movies controller here, then:

  # this will show a single movie, which is where I am guessing
  # you would want to show the actors in that movie and add new actors
  def show
    @movie = Movie.find(params[:id])
  end

  # When the user clicks the "add actor" link on the show page
  # the will be taken here to fill in actor details
  def new_actor
    @movie = Movie.find(params[:id])
    @actor = @movie.actors.build
  end

  # When the user clicks the save button on the add actor page
  # they will be taken here and eventually back to the show page
  def create_actor
    @movie = Movie.find(params[:id])
    @actor = @movie.actors.build(params[:actor])
    if @actor.save
      flash[:notice] = "Your actor was successfully added."
      redirect_to :action => 'show', :id => @movie.id
    else
      render :template => "new_actor"
    end
  end
end


Here are some very basic views:

/* show.html.erb */
<h1><%= @movie.title %></h1>
<ul>
<% @movie.actors.each do |actor| %>
  <li><%= actor.name %></li>
<% end %>
</ul>
<%= link_to "add actor", :action => 'new_actor', :id => @movie.id %>

/* new_actor.html.erb */
<h1>New Actor for <%= @movie.title %></h1>
<% form_for :actor, @actor, :url => {:action => 'create_actor', :id => @movie.id} do |f| %>
  <label for='actor_name'>Name</form><br />
  <%= f.text_field :name %>
  <%= submit_tag "save" %>
<% end %>


You could do virtually the same thing with a separate ActorsController, just put the new_actor and create_actor actions in that controller, then, anytime you are referencing those actions in other controllers and their views, make sure you specify :controller => :actors along with the :action => ...

Let us know if you have any more questions, there are a ton of very helpful people here just waiting.

Re: implementing has_many relationships

THANK YOU THANK YOU THANK YOU THANK YOU TAHNK YOU

YAHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

THANK YOU THANK YOU THANK YOU


THANK YOU THNK YOU


THANK YOU.


after 4 months it worked. all because of you. thanks

big_smile big_smile big_smile big_smile big_smile big_smile big_smile big_smile big_smile big_smile

Re: implementing has_many relationships

oh wait... lol sorry im getting an error.

ActiveRecord::RecordNotFound in PagesController#create

Couldn't find Course without an ID



What??? This is my pages controller--

def create
    @course = Course.find(params[:id])
    @page = @course.pages.build(params[:page])

if.... else.... 

end

and my form for submitting that page--

<% form_for [@course, Page.new] do |f| %>

.....



And my pages belongs to course, and course has many pages.

Do you know why I'm getting this error? Usually whats the probleM? Thanks

IMPORTANT--

-------------

P.S. Ohhh this might be a key factor to my error- right now, if a user clicks on a Course, it brings them to the SHOW action of the COURSE controller. In the show page, i have a LINK_TO new_page_path... now.. does Rails know what Course I'm in when i click on that? or do i need to specify parameters in the LINK_tO, telling rails which course I'm in before creating the page?

=======

Thanks!

Re: implementing has_many relationships

this forum really is helpful in helping me figure out a lot of things - thanks! I have a lot of reading to do

Re: implementing has_many relationships

Try changing it to:

@course = Course.find(params[:course_id]).

If that doesnt work, view the page source when you have the new form rendered, and look for the course id hidden field. See what it's name attribute is set to. For instance if you have a name of    user[name], that's stored as: params[:user][:name]. However, if its just   user_id, then you would access it by: params[:user_id]

Re: implementing has_many relationships

thanks guys, this time it really worked- http://www.akitaonrails.com/2007/12/12/ … l-tutorial  that helped me a lot

Re: implementing has_many relationships

Very useful information, congratulations for the post and help.

Re: implementing has_many relationships

Thanks for letting me know about other good stuff ! smile

Last edited by dara11 (2011-10-11 02:28:36)

Re: implementing has_many relationships

Thanks for sharing that site. It works for me,,,

an admin from Sydney Plumber

Last edited by jameskingama (2012-03-09 18:01:26)