Guest User

Untitled

a guest
Aug 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. ## book.rb
  2. class Book < ActiveRecord::Base
  3. has_many :book_people, :dependent => :destroy
  4. has_many :people, :through => :book_people, :uniq => true
  5.  
  6. has_many :editors, :class_name => 'Person', :finder_sql => <<-SQL
  7. SELECT people.* FROM people
  8. INNER JOIN book_people ON (book_people.person_id = people.id AND people.type = 'Editor')
  9. WHERE book_people.book_id = #{id}'
  10. SQL
  11.  
  12. has_many :editors, :class_name => 'Person', :finder_sql => <<-SQL
  13. SELECT people.* FROM people
  14. INNER JOIN book_people ON (book_people.person_id = people.id AND people.type = 'Author')
  15. WHERE book_people.book_id = #{id}'
  16. SQL
  17. end
  18.  
  19. ##person.rb
  20. class Person < ActiveRecord::Base
  21. has_many :book_people, :dependent => :destroy
  22. has_many :books, :through => :book_people
  23. end
  24.  
  25. ##book_person.rb
  26. class BookPerson < ActiveRecord::Base
  27. belongs_to :person
  28. belongs_to :book
  29.  
  30. belongs_to :author, :class_name => "Person", :foreign_key => :person_id
  31. belongs_to :editor, :class_name => "Person", :foreign_key => :person_id
  32. end
Add Comment
Please, Sign In to add comment