0

I have a Author model that has_many Books.

I've overridden Books to be able to be found by their title

class Book < ActiveRecord::Base    
...
 def self.find(input)
    if input.is_a?(Integer) || input.integer?
      super(input)
    else
      return Book.find_by_title(input)
    end
  end
...
end

This works fine in most cases, but if I do something like author.books.find("Some Book Title") this method doesn't seem to be called.

Is there any way to fix this?

you786
  • 3,659
  • 5
  • 48
  • 74

1 Answers1

1

Since you are going through the books collection, you are actually calling find on a CollectionProxy. You can extend the methods from a has_many by passing a block to it like the answer says in this thread: Rails: Overriding ActiveRecord association method

Community
  • 1
  • 1
Logan Serman
  • 29,447
  • 27
  • 102
  • 141