首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何让索引水箱来做条件索引?

我如何让索引水箱来做条件索引?
EN

Stack Overflow用户
提问于 2011-05-18 04:48:58
回答 1查看 196关注 0票数 0

在与IndexTank的优秀人员进行了长时间的交谈后,我真的不确定如何解决我的问题,我想知道是否有人可以帮助我。

我有一个属于用户模型的文章模型。该文章模型还有一个名为匿名的布尔属性,如果设置为true,则用户可以选择在不显示其姓名的情况下发布文章。

代码语言:javascript
复制
Article
belongs_to :user
attr_accesible :anonymous, :user_id

User
has_many :articles

我的问题是,如果文章是匿名发布的。我不希望tanker在作者姓名字段中搜索,但我希望它搜索所有其他字段。我试着用if else语句来做这件事,我通常会把"tankit“块放在那里,但这不起作用。

有没有一种方法可以把tankit块放到model方法中,并像这样使用验证回调?

代码语言:javascript
复制
def anon_index
    if self.anonymous
      tankit 'my_index' do
        indexes VARIABLES ETC BUT NOT the user_ attributes
      end
    else # if anonymous is false
      tankit 'my_index' do
        indexes :title
        indexes :body
        indexes :user_penname
        indexes :user_firstname
        indexes :user_lastname
      end
    end
  end

我想要么这样,要么在"tankit“块声明所在的地方放一个if else语句,但这两个似乎都不是,除非我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-18 05:30:08

这看起来怎么样?:

代码语言:javascript
复制
class Article < ActiveRecord::Base

 tankit 'my_index' do
       indexes :title
       indexes :body
       indexes :custom_penname
       indexes :custom_firstname
       indexes :custom_lastname
 end

 def custom_penname
   if self.anonymous
     'anonymous'
   else
     self.user_penname
   end
 end

 def custom_firstname
   #same for first name
 end
 def custom_lastname
   #same for last name
 end
end

相同的方法,不同的场景:

https://github.com/adrnai/rails-3-tanker-demo/blob/master/app/models/comment.rb

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6036956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档