Missing documentation for find_in_collection? Trying to dril


#1

I’ve been reading up in ActiveRecord::Associations and playing around
with my app via the console and found some interesting methods via
the code completion of IRB, but I have no idea how to use them and
can only find one hint in the API
http://api.rubyonrails.com/classes/ActiveRecord/Associations/
ClassMethods.html

It mentions here in the has many associations that if you use the
finder_sql that you would loose the find_in_collection. What are
these and the find_all_in_collection methods I am finding. For
example the simple models i am using is a 4-tier has_many structure
like this:

class User < ActiveRecord::Base
has_many :foos,
end

class Foo < ActiveRecord::Base
belongs_to :user
has_many :bars,
end

class Bar < ActiveRecord::Base
belongs_to :foo
has_many :mistakes,
end

class Mistake < ActiveRecord::Base
belongs_to :bar
end

So while in IRB via the console of my app, I can do this and see all
the following options via command completion on the next step.

@user = User.find(1)
=> #<User:0x26eb9cc @attributes={…

@user.find
@user.find
@user.find_collection_for_pagination
@user.find_mapped_obj_class @user.find_tag
@user.find_all
@user.find_first
@user.find_mapped_soap_class @user.find_target
@user.find_all_in_foos
@user.find_first_recursive
@user.find_name @user.find_user_object
@user.find_all_tag
@user.find_in_foos
@user.find_plugins
@user.finding_with_ambigious_select?

@user.find_in_foos(1)
=> #<Foo:0x26a0008 @attributes={

It’s really crazy, I can use that method @user.find_in_foos(1) and
get an object that belongs to the user and once that is done, it
opens up more find commands (specifically find_all_in_bars and
find_in_bars) to the next layer in my tier. So now I want to try and
find an associated object to the user by using the new methods. But
it fails if the association is more than 1 deep. So for example…

@user.find_in_bars(1)
NoMethodError: LOG: duration: 468.984 ms statement: SELECT * FROM
columns WHERE (foos.user_id = 1) ORDER BY position
undefined method find_in_bars' for #<User:0x26eb9cc> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/ lib/active_record/base.rb:1792:inmethod_missing’
from (irb):3

These methods look like they would be fun to use. Ultimately I just
want to find an object thru an association starting at the top. Any
feedback advise?

  • Ken