1

我的搜索中有几个条件。

@events = Event.search(params[:search], 
  :conditions => {:group_size => 1, :days => 1})

上面的代码工作正常。但是,如果我想用私有方法替换散列,我会收到语法错误

syntax error, unexpected ',', expecting tASSOC
    :conditions => {group_size_condition, :days => 1},

代码如下

@events = Event.search(params[:search], 
  :conditions => {group_size_condition, :days => 1})

private

def group_size_condition
  if params[:groupsize] == 'single (1)'
    :group_size => 1
  elsif params[:groupsize] == 'couple (2)'
    :group_size => 2
  elsif params[:groupsize] == 'small group(3-5)'
    :group_size => 3..5
  else
    nil
  end
end

提前致谢

4

3 回答 3

2

那将是工作

@events = Event.search(params[:search], 
  :conditions => group_size_condition.merge(:days => 1))

private

def group_size_condition
  case params[:groupsize]
  when 'single (1)' then {:group_size => 1}
  when 'couple (2)' then {:group_size => 2}
  when 'small group(3-5)' then {:group_size => 3..5}
  else
    {}
  end
end
于 2012-08-12T19:43:38.283 回答
0

我想你错过了 :group_size 键

@events = Event.search(params[:search], 
  :conditions => {:group_size => group_size_condition, :days => 1})
于 2012-08-12T19:11:34.727 回答
0

我想也许你想实际传递字符串,如:

def group_size_condition
  if params[:groupsize] == 'single (1)'
    ':group_size => 1'
  elsif params[:groupsize] == 'couple (2)'
    ':group_size => 2'
  elsif params[:groupsize] == 'small group(3-5)'
    ':group_size => 3..5'
  else
    nil
  end
end
于 2012-08-12T19:15:39.883 回答