Hello,
If i have a has_many and an HABTM relationship what is the best way to manage this? I.E.,
user
Hello,
If i have a has_many and an HABTM relationship what is the best way to manage this? I.E.,
user
use something like the following (untested)
user.rb
# each user can share a number of books has_and_belongs_to_many :shared_books, :join_table => 'shared_books', :class_name => 'book'
book.rb
# a book can belong to many users has_and_belongs_to_many :shared_users, :join_table => 'shared_books', :class_name => 'user'
Mike
thanks I will try this. I wasnt sure if you can do
has_many :books has_and_belongs_to_many :shared_books, :join_table => ‘shared_books’, :class_name => ‘book’
in the same model.
Adam
unfortunately this didnt work… i had this in my User model
has_many :books has_and_belongs_to_many :shared_books, :join_table => ‘shared_books’, :class_name => :book
and this in the book model
belongs_to :user has_and_belongs_to_many :shared_books, :join_table => ‘shared_books’, :class_name => :user
TypeError: can’t convert Symbol into String
from /usr/lib/ruby/gems/1.8/gems/activerecord-
1.15.3/lib/active_record/base.rb:1087:in type_name_with_module' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1356:in
compute_type’
from /usr/lib/ruby/gems/1.8/gems/activerecord-
1.15.3/lib/active_record/reflection.rb:125:in klass' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_and_belongs_to_many_association.rb:158:in
construct_sql’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_and_belongs_to_many_association.rb:6:in initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:934:in
shared_books’
from (irb):2