views:

330

answers:

2

How do I update additional data in HABTM tables.

For Example: I have movies, people and HABTM movies_people tables, but there is additional persontype_id field in movies_people table which indicates role of this person in that particular movie. How do I add/change this value?

+3  A: 

You should probably be using a has_many :through association, which was introduced just for this reason. Instead of just a movies_people table, you'd have an additional model, called somthing like Favorite or Viewing (depending on what you're trying to accomplish with the association), which belongs_to both :movies and :people, and in your Movie model, you'd do

has_many :favorites
has_many :people, :through => :favorites

This gives you access to movie.people for all the people who have favored this movie and movie.favorites to access anything that would be in your favorites table (like persontype_id)

Josh Susser's Article, Many-to-Many Dance Off does a much better job of explaining this that I could ever hope, so for additional help, I'd take a look at it. http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off

PJ Davis
+2  A: 

There is also a great Railscast on the subject, by Ryan Bates:

http://railscasts.com/episodes/47-two-many-to-many

Josh