static_model icon indicating copy to clipboard operation
static_model copied to clipboard

belongs_to doesn't work for rails 3.2.2

Open djiang opened this issue 13 years ago • 1 comments

This association doesn't seem to work anymore:

Lets say our Books have many Readers. Book is our StaticModel, but a Reader is an ActiveRecord model.

class Reader < ActiveRecord::Base
  belongs_to :book, :foreign_key => 'book_id'
end

I get the following error:

NoMethodError: undefined method `scoped' for Book:Class
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/bundler/gems/static_model-1f2beb10aba5/lib/static_model/base.rb:210:in `method_missing'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:123:in `target_scope'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/singular_association.rb:42:in `find_target'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:145:in `load_target'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:55:in `reload'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/singular_association.rb:9:in `reader'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
    from (irb):4
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
    from /Users/david/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

djiang avatar Mar 06 '12 03:03 djiang

I have a similar issue, but with activerecord (4.2.4)

NoMethodError: undefined method `arel_table' for Reader:Class

User code quick fix:

# reader.rb
def book
    Book.find(self.book_id)
  end

allavena avatar Nov 12 '15 04:11 allavena