Is there any way to set a field name that is different than the method name used to populate it? My example: In Rails, I''m using acts_as_ferret on a Article model that has_many Authors. I want to be able to index authors'' names with each article. Since @article.authors yields an array of Author objects (and not an indexable string), I''ve created this method in my Article model: def authors_aaf @authors.collect {|a| a.name }.to_sentence end I call it "authors_aaf" because "authors" would cause a conflict. But I want my field name to be "authors", because that makes the most sense. (Users shouldn''t have to type "authors_aaf:Joe" to search by author.) Any idea how this can/should be done? -- Posted via http://www.ruby-forum.com/.
On Mon, Oct 08, 2007 at 04:30:12AM +0200, Chase DuBois wrote:> Is there any way to set a field name that is different than the method > name used to populate it? > > My example: > > In Rails, I''m using acts_as_ferret on a Article model that has_many > Authors. I want to be able to index authors'' names with each article. > Since @article.authors yields an array of Author objects (and not an > indexable string), I''ve created this method in my Article model: > > def authors_aaf > @authors.collect {|a| a.name }.to_sentence > end > > I call it "authors_aaf" because "authors" would cause a conflict. But I > want my field name to be "authors", because that makes the most sense. > (Users shouldn''t have to type "authors_aaf:Joe" to search by author.) > > Any idea how this can/should be done?You could pass the query through a de-aliasing helper method - something that does query.gsub!(/authors:/, ''authors_aaf:'') - before handing off the query string to ferret. -Chris
Apparently Analagous Threads
- Conditional pluralize without the number
- [ActiveSupport] Patches needing review: re-organization of #to_query methods, preservation of XML/JSON content-type
- Eager loading wierdness
- How do you use acts_as_ferret without rails?
- acts_as_ferret: can i specify a search on 1 field as suppose