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.

Anybody???.........

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.

Thanks very much...

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.