limitations of :finder_sql
-
21-08-2019 - |
Question
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?
Solution
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
OTHER TIPS
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.