1

I have several conditions in my search.

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

The above code is working fine. However, if I want to replace the hash with a private method, I get syntax error

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

Code is as follow

@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

Thanks in advance

3 Answers 3

2

That will be work

@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
Sign up to request clarification or add additional context in comments.

Comments

0

I think you missed :group_size key

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

2 Comments

I think I also need to change the private method. It should only return the value, instead of the entire condition.
an irrelevant question, @pkubicki. basically I want to ignore the groupsize search condition if groupsize fall into 'else' condition. It looks like nil is not correct in this case. Do you know how to achieve this goal?
0

I think perhaps you want to actually pass the string, as in:

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

1 Comment

thanks for the quick turnaround. But the above code does not work. Still get the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.