Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## book.rb
- class Book < ActiveRecord::Base
- has_many :book_people, :dependent => :destroy
- has_many :people, :through => :book_people, :uniq => true
- has_many :editors, :class_name => 'Person', :finder_sql => <<-SQL
- SELECT people.* FROM people
- INNER JOIN book_people ON (book_people.person_id = people.id AND people.type = 'Editor')
- WHERE book_people.book_id = #{id}'
- SQL
- has_many :editors, :class_name => 'Person', :finder_sql => <<-SQL
- SELECT people.* FROM people
- INNER JOIN book_people ON (book_people.person_id = people.id AND people.type = 'Author')
- WHERE book_people.book_id = #{id}'
- SQL
- end
- ##person.rb
- class Person < ActiveRecord::Base
- has_many :book_people, :dependent => :destroy
- has_many :books, :through => :book_people
- end
- ##book_person.rb
- class BookPerson < ActiveRecord::Base
- belongs_to :person
- belongs_to :book
- belongs_to :author, :class_name => "Person", :foreign_key => :person_id
- belongs_to :editor, :class_name => "Person", :foreign_key => :person_id
- end
Add Comment
Please, Sign In to add comment