•
•
•
•
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
![]() |
•
•
Join Date: Jun 2007
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 1
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.
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.
•
•
Join Date: Oct 2005
Location: Manchester, UK
Posts: 482
Reputation:
Rep Power: 4
Solved Threads: 34
•
•
•
•
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
•
•
Join Date: Jan 2005
Location: San Jose, CA
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 1
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".
-
Ed
- 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Ruby Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Searching multiple MS SQL Tables (VB.NET)
- How to include multiple tables in FM format (MS Access and FileMaker Pro)
- Query multiple tables with duplicate data (MySQL)
- Searching for a record in multiple tables (VB.NET)
- Multiple Tables in a Database (Visual Basic 4 / 5 / 6)
- Delete from multiple tables (MySQL)
- Query multiple tables? (MySQL)
- updating 2 HTML tables on one PHP page (PHP)
Other Threads in the Ruby Forum
- Previous Thread: install plugin
- Next Thread: My first Ruby programming question :-)


Linear Mode