函数输入参数的未定义方法错误

时间:2015-08-15 14:25:07

标签: ruby-on-rails ruby

我收到以下错误:

ShowsQueryObject querying for a show by start date returns shows that are after the start date
     Failure/Error: expect(ShowsQueryObject.query(start_date: start_date)).to match_array [after_start_date_show_1, after_start_date_show_2]
     NoMethodError:
       undefined method `query_params' for ShowsQueryObject:Class

对于此代码:

class ShowsQueryObject
  class << self
    def query(query_params)
      Show.where(query_string(query_params), query_values(query_params))
    end

    private

    def query_string(query_params)
      query_string = []
      query_string << start_date_query if query_params(:start_date)
      query_string << end_date_query if query_params(:end_date)
      query_string << artist_name_query if query_params(:artist_name)
      query_string << venue_id_query if query_params(:venue_id)
      query_string.join(' AND ')
    end

    def query_values(query_params)
      {}.tap do |hash|
        hash[:start_date] = query_params(:start_date) if query_params(:start_date)
        hash[:end_date] = query_params(:end_date) if query_params(:end_date)
        hash[:artist_name] = query_params(:artist_name) if query_params(:artist_name)
        hash[:venue_id] = query_params(:venue_id) if query_params(:venue_id)
      end
    end

    ...
  end
end

我猜这与Ruby中的私有静态方法有关?这是我第一次搞砸class << self所以我假设我做错了什么,但从我在网上找到的内容来看,这一切对我来说都是合法的。

1 个答案:

答案 0 :(得分:2)

你应该尝试将query_params(:start_date)更改为query_params [:start_date],因为如果你把它放在“()”中,ruby将它作为方法而不是属性