2

In a rails app, I using the :finder_sql option in a has_many declaration. The rails docs say that when I do this, "find_in_collection is not added." What does this mean?

allyourcode
  • 21,871
  • 18
  • 78
  • 106

2 Answers2

3

it means that when you have a has_many relationship between to tables like:

person has_many :books, :finder_sql

you wont get the person.books.find* methods you normaly would have gotten. the reason for this behavior is that activerecord cant easily compose the finder_sql with the find* methods you would be using, so it cant really give you that functionality

LDomagala
  • 2,193
  • 4
  • 20
  • 34
3

This means that it doesn't support the method for finding an instance within a collection. The docs call it find_in_collection (where "collection" is whatever the name of your association). An example might be more helpful here:

class Author < ActiveRecord::Base
  has_many :posts

  has_many :special_posts, :class_name => "Post", 
           :finder_sql => "SELECT * FROM posts WHERE ..."
end

author.find_in_posts(30)          # it finds and returns post 30
author.find_in_special_posts(30)  # not supported because finder_sql is used here.
Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
  • Hi avit, I'm not sure what you meant by "the method for finding an instance within a collection find_in_collection". In fact, I wasn't even sure how to parse that. Otherwise, your example would have made your answer better than Cyrik's. Dan – allyourcode Feb 06 '09 at 02:29
  • Edited... hopefully that makes more sense now: I was referring to what the method was called in the docs, when it's actually a metaprogrammed method based on the name of your collection. – Andrew Vit Feb 06 '09 at 19:35