magic_join model creation and rails 2 October 7th, 2008
If you are using Josh Susser’s clean magic_join solution to automagically create a join record an entry on a join table, you might well notice it doesn’t work with rails 2:
protected method `with_scope' called for #<Class:0x2109ddc>
Indeed the with_scope method is now protected.
In lieu of:
class Contributor < ActiveRecord::Base
has_many :contributions, :dependent => :destroy
has_many :books, :through => :contributions do
def push_with_attributes(book, join_attrs)
Contribution.with_scope(:create => join_attrs) { self << book }
end
end
end
Do:
class Contributor < ActiveRecord::Base
has_many :contributions, :dependent => :destroy
has_many :books, :through => :contributions do
def push_with_attributes(book, join_attrs)
Contribution.send(:with_scope, :create => join_attrs) { self << book }
end
end
end
/ Tags: Ruby on Rails, Design, Code Trackback