Bendegúz
Bendegúz

Reputation: 766

RoR: Model Associations

(sorry for my english) If i have 3 model, :Movie :Actor :Connect, how can i make id Assocations? Connect model have a movie_id:integer and a actor_id:integer, and i wanna make connections between actors and movies.

Upvotes: 0

Views: 61

Answers (2)

Kashif Umair Liaqat
Kashif Umair Liaqat

Reputation: 1074

Here you go.

in Movie model:

class Movie < ActiveRecord::Base
  has_many :connects
  has_many :actors, :through => :connects
end

in Actor model:

class Actor < ActiveRecord::Base
  has_many :connects
  has_many :movies, :through => :connects
end

in Connect model:

class Connect < ActiveRecord::Base
  belongs_to :movie
  belongs_to :actor
end

Upvotes: 2

sameera207
sameera207

Reputation: 16629

It seems like you want to do a HABTM (Has And Belongs To Many) relationship

check these links on the same

1) rails api

2) rails cast (free but old), and paid

3) one my my sample project on HABTM

Upvotes: 0

Related Questions