找到你要的答案

Q:Undefined method error for input parameters to a function

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

I am getting the following error:

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

For this code:

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

I am guessing this has something to do with private static methods in Ruby? This is my first time messing around with class << self so I'm assuming I did something wrong, but from what I can find on line this all looks legit to me.

我得到以下错误:

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中的私有静态方法有关系吗?这是我第一次玩类<;<;自我想我做错了什么,但我可以找到在线这一切看起来合法的我。

answer1: 回答1:

You should try changing query_params(:start_date) to query_params[:start_date], because if you put it with "()" ruby takes it as a method and not a property

你应该试着改变query_params(:start_date)来query_params [:start_date ],因为如果你把它用“()”的红宝石作为方法而不是财产

ruby-on-rails  ruby