User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Ruby section within the Web Development category of DaniWeb, a massive community of 456,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Ruby advertiser: SELL YOUR PRODUCT TODAY !
Views: 4449 | Replies: 4
Reply
Join Date: Jun 2007
Posts: 16
Reputation: echobase is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
echobase echobase is offline Offline
Newbie Poster

Multiple db tables in Rails...

  #1  
Oct 29th, 2007
Hello-

As a newbie Rails developer, I realize I should have a solid knowledge of SQL regardless of the wonderful abstraction Rails provides. That said, I'm trying to build my first app within the next couple of weeks, relying completely on those abstractions. A couple of questions......

Let's say I have two entities, Movie and Documentary, that I need to make tables/models for. Each of these has Actor(s) and I will make a table/model for that as well. So, in the Actors model can I include more than one ":belongs_to" statement, in this case one each for Movies and Documentaries?

Also, does the use of statements like ":has_many" and "belongs_to" completely eliminate the need to type in "join" instructions in SQL?

Thanks for your help.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 16
Reputation: echobase is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
echobase echobase is offline Offline
Newbie Poster

Re: Multiple db tables in Rails...

  #2  
Nov 2nd, 2007
Anybody???.........
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 482
Reputation: pty is on a distinguished road 
Rep Power: 4
Solved Threads: 34
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: Multiple db tables in Rails...

  #3  
Nov 9th, 2007
Originally Posted by echobase View Post
Hello-

As a newbie Rails developer, I realize I should have a solid knowledge of SQL regardless of the wonderful abstraction Rails provides. That said, I'm trying to build my first app within the next couple of weeks, relying completely on those abstractions. A couple of questions......

Let's say I have two entities, Movie and Documentary, that I need to make tables/models for. Each of these has Actor(s) and I will make a table/model for that as well. So, in the Actors model can I include more than one ":belongs_to" statement, in this case one each for Movies and Documentaries?

Also, does the use of statements like ":has_many" and "belongs_to" completely eliminate the need to type in "join" instructions in SQL?

Thanks for your help.


You can add as many :has_many and :belongs_to statements to each model as you like.

Then you can access the associated like this:

#in controller
def view
  @actor = Actor.find(1)
end


#and in view.rhtml
<% @actor.movies.each do |m| %>
  <li><%= m.title %></li>
<% end %>

Also remember that actors/movies and actors/documentaries are many to many relationships (has_and_belongs_to_many) in rails. You'll find more info about many to many on the rails website.
Note to self... pocket cup
Reply With Quote  
Join Date: Jun 2007
Posts: 16
Reputation: echobase is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
echobase echobase is offline Offline
Newbie Poster

Re: Multiple db tables in Rails...

  #4  
Nov 9th, 2007
Thanks very much...
Reply With Quote  
Join Date: Jan 2005
Location: San Jose, CA
Posts: 3
Reputation: eruder is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
eruder eruder is offline Offline
Newbie Poster

Re: Multiple db tables in Rails...

  #5  
Jan 1st, 2008
Couple of observations:

- Depending on how different "movies" and "documentaries" are, you may want to consider single-table inheritance--i.e., putting both in one table with a "type" column distinguishing the two and perhaps some columns that only apply to one or the other type of "film".

- has_and_belongs_to_many is one way to model the relationship, as pty points out. If the association might want to have additional information associated with it, you should use a has_many :through association. The join table that represents the relationship between the two main tables--"movies" and "actors", in your case--has its own model class, methods, etc. You might call it "roles" in this case, and have information like the name of the character the actor played, etc., in each record.

Ed
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Ruby Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Ruby Forum

All times are GMT -4. The time now is 6:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC