Acts_as_ferret with STI models

Can acts_as_ferret search only one of the inherit models in the
hierarchy of STI models? Say you have Contents, with types articles and
comments. I know that you do Contents.find_by_contents, but can you
also create indexed for Comment and Articles?

Thanks for you help

Miguel

You can most definately do this. Just index your base class in the STI
model. Then you can run “find_by_contents” from any of the children.

I created a method in my base STI class so I can scope my query. For
scoping
I used something like the following line:

query << " role:#{self.class.eql?(Contents) ‘*’ : self.class}"

Though you could make it more generic by simply asking
“self.descends_from_active_record?” which is how rails decides if it
should
scope your “find” query for STI models. You can check out “base.rb” in
activerecord to see that.

I do believe that eventually AAF will scope queries for STI as I see a
TODO
in the source about it but Jens would be a better person to comment on
that.

One last note, if you are using Rails 1.1.6 or earlier and plan on using
something liked CachedModel (an abstract class that sits between
ActiveRecord::Base and the base of your STI model) please see my email
“Strange indexing issues with CachedModel, STI, and AAF” as Rails 1.1.6
does
not properly scope queries for STI that have an abstract_class parent.

Hope that helps,
Curtis

----- Original Message -----
From: “Miguel” [email protected]
To: [email protected]
Sent: Monday, November 20, 2006 8:15 PM
Subject: [Ferret-talk] acts_as_ferret with STI models

Thanks Curtis for you quick response! I do have another question.

query << " role:#{self.class.eql?(Contents) ‘*’ : self.class}"

Does AAF look for a field called “role” by default? Or is :role a field
that is added to any STI model? Shouldn’t that be :type?

Thanks!

Miguel

Ahhh… sorry about that. I redefined the field name for my STI because
“type” causes way to many problems.

class YourModel < ActiveRecord::Base
… code …

def inheritance_column
‘role’
end
end

You may come up with a better column name than that but I recommend
redefining it because you can’t do record.type without getting
warnings/errors (it caused problems elsewhere for me as well). Currently
AAF
does not scope queries for you (to my knowledge).

Hope that clears up the confusion I may have caused you,
Curtis

a link to rails doc explaining what I just said:
http://rubyonrails.com/rails/classes/ActiveRecord/Base.html#M000879

----- Original Message -----
From: “Miguel” [email protected]
To: [email protected]
Sent: Monday, November 20, 2006 10:38 PM
Subject: Re: [Ferret-talk] acts_as_ferret with STI models

On Mon, Nov 20, 2006 at 09:34:54PM -0500, Curtis Hatter wrote:

scope your “find” query for STI models. You can check out “base.rb” in
activerecord to see that.

I do believe that eventually AAF will scope queries for STI as I see a TODO
in the source about it but Jens would be a better person to comment on that.

AAF does no scoping for searches on an STI model, but as AAF uses
activerecord to retrieve the results delivered by find_by_contents, the
usual Rails scoping will take place, so you’ll get only instances of the
class you called find_by_contents on.

However the :limit and :offset options of find_by_contents become quite
useless since they will be applied to the (unscoped) result set from
ferret, and the scoping will only take place later when retrieving the
actual records from the db.

So scoping the ferret query is a good idea, and it should be possible to
integrate that into aaf once I find the time do make a new release…

cheers,
Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

Thank you so much Curtis. That was exactly what i needed to know!!!
your awesome

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs