2012-04-06 143 views
0

行,所以我有一个日期字段,我需要搜索,但我需要在今天就像在一个MySQL查询如何使用attr_accessor进行搜索?

search_conditions << ["DAY(open_date) != ?", event.thursday.day] if options[:thur].blank? 

它来搜索,我需要做的,所以我试图思考狮身人面像这种情况这

attr_accessor :event_day 

    def event_day 
    self.start_date.day 
    end 

    #thinking sphinx configurations for the event search 
    define_index do 
    indexes event_day 
    ... 
    ... 

,并在搜索我想这

search_string = "@event_day -#{event.thursday.day}" unless options[:thur].blank? 

但我不断收到此错误

index event_core: query error: no field 'event_day' found in schema 

什么办法,使这项工作

回答

1

在SQL查询中不能使用红宝石属性。 Rails并不聪明。

您需要编写复制该函数的SQL或通过它过滤查询结果,例如,

@my_query.where(:a => "b").select { |rec| rec.some_method == "some value" } 
+0

不知道如何解决这种情况...我创建一个方法,返回一天 – Trace 2012-04-06 16:12:53

0

正如Michael所指出的,Sphinx无法访问Ruby属性 - 它会直接与您的数据库进行通信。

因此,无论您是否可以创建一个包含事件日值的列,并通过Sphinx引用该列,或者您可以创建一个使用SQL函数的字段(可能因MySQL或PostgreSQL而异)从start_date列开始 - 不是特别复杂。它可能最终会看起来像这样:

indexes "GET_DAY_FROM_DATE(start_date)", :as => :event_day