theKindOfMe

March 5, 2009

Rails ActiveRecord: Having Multiple Relationships within Two Models

Filed under: Uncategorized — Tags: , , , , — yasi8h @ 3:03 am

noob alert!: I am pretty new to RoR. so i might not be solving the problem with the best possible solution.

Scenario 1: many-to-many + one-to-many

Task: I have two models. Contact and ContactSource. a Contact belongs to a ContactSource. and a Contact can be synced with one or more ContactSource(s). i need to get the ContactSource(s) with whom a given Contact is synced ex:- Contact1.synced_with => [ContactSource1, ContactSource2…]

Solution:  for the first relationship(one-to-many) we can use belongs_to. and for second we can use has_many with :through

class Contact :contacts_synced_with_contact_sources

end

class ContactsSyncedWithContactSource < ActiveRecord::Base belongs_to :contact belongs_to :contact_source end [/sourcecode] In the above code we have no problem accessing the data relevant to the two relationships. because: Contact.contact_source would point to the Contact Source who owns the Contact while Contact.contact_sources would point to the ContactSources with whom the Contact is synced with. but look at the next problem.... Scenario 2: many-to-many + many-to-many Task: I have two models. Book and Student. a student can own many books, while a book(this does not refer to a individual copy of a book, but to all the copies of a book) can be owned by many students. and a student have a collection of books that he loves. Problem: here we have two many to many relationships within two business entities. if you try to solve this with has_many :through or has_and_belongs_to_many, you would run in to a problem. in the first problem you referred to the ContactSources by using the property accessor contact_sources. but here if you try using student1.books how would ActiveRecord know what to return?(book the student love or the books he own?). you could solve this problem by using the :source option(is that what you call these in ruby? :P) in has_many... it would like this... has_many :books_i_love, :source => “book”, :through => :student_love_books

you can give a custom property accessor name such as books_i_love but then you have to use the :source => “book” to let rails know about what model(s) you are trying to access.

if you do not use the :source => “book” you would get a very helpful error message like this

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :books_i_love or :books_i_love in model StudentLoveBook. Try 'has_many :books_i_love, :through => :student_love_books, :source => '. Is it one of :student or :book?

source follows…

class Student “book”, :through => :student_love_books

has_many :student_own_books
has_many :books, :through => :student_own_books
end

class Book < ActiveRecord::Base end class StudentLoveBook < ActiveRecord::Base belongs_to :student belongs_to :book end class StudentLoveBook < ActiveRecord::Base belongs_to :student belongs_to :book end class CreateBooks < ActiveRecord::Migration def self.up create_table :books do |t| t.string :name t.timestamps end end def self.down drop_table :books end end class CreateStudents < ActiveRecord::Migration def self.up create_table :students do |t| t.string :name t.timestamps end end def self.down drop_table :students end end class CreateStudentOwnBooks < ActiveRecord::Migration def self.up create_table :student_own_books do |t| t.column :student_id, :integer t.column :book_id, :integer t.timestamps end end def self.down drop_table :student_own_books end end class CreateStudentOwnBooks < ActiveRecord::Migration def self.up create_table :student_own_books do |t| t.column :student_id, :integer t.column :book_id, :integer t.timestamps end end def self.down drop_table :student_own_books end end [/sourcecode] hope this would help someone/myself in the future 🙂

Blog at WordPress.com.