Too Cool for Internet Explorer

acts_as_ferret on associations

acts_as_ferret-on-associations

Just before anyone else runs into this ยง$%&/, I’ll do a quick post:

Let’s assume you have a rails app, want to add full-text search via Jens’ great acts_as_ferret(ferret) plugin and you’d also like to have attributes from multiple models in ONE index. In this case you probably found the same resources on the net as I did:

All of these are really great tutorials, with just a little flinch that I (!!) missed the whole day:

THEY all have one to one or one to many associations, but in any case do the cross indexation on the model which has exactly one associated row, with something like this:


class Book < ActiveRecord::Base
acts_as_ferret :additional_fields => [:author_first_name,
:author_surname]
belongs_to :author
###
def author_first_name
return author.first_name
end
###
def author_surname
return author.surname
end
end

I (!!) did the whole thing on the model which can have multiple associated items on it and (i know this may be a little embarassing) missed the following line, wondering how this could possibly work for ME in the first place, because of the magic-undefined-instance variable:


def job_title
return job.title
end

What you have to do is this:


def job_title
jobs.collect { |job| job.title }.join ' '
end

Note the plural in here - I’ll give you a sec - and now you’re probably laughing ;)

So - as i said, just in case - I wanted to share this little secret with those people out there, beeing as rubber ducked as me.

cheers, Nils

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with GimpStyle Theme design by Horacio Bella. Get Entries and comments.