conditional statements
I want to do a customised search through a method in my model. One of the search conditions looks like this:
left = params[:left][:left_id]
开发者_JAVA百科 right = params[:right][:right_id]
status = give_status(left, right)
where << "status_id_number = #{status}"
The params come from two drop down menus. My problem is with setting the value for status. If I do something like this it correctly sets the value from the left param:
def self.give_status(left, right)
return left
end
I would like to have it setup like below. Where the left and right variables are compared and then a certain return value is assigned based on the conditions. The code below will return 2, even if I set left and right set to 1.
def self.give_status(left, right)
if left == 1 and right == 1
return 1
else
return 2
end
end
problem is that your param
will return strings
def self.give_status(left, right)
if left.to_i == 1 and right.to_i == 1
1
else
2
end
end
Short form
def self.give_status(left, right)
left.to_i == 1 && right.to_i == 1 ? 1 : 2
end
or get it right in the beginning:
left = params[:left][:left_id].to_i
right = params[:right][:right_id].to_i
status = give_status(left, right)
where << "status_id_number = #{status}"
def self.give_status(left, right)
left == 1 && right == 1 ? 1 : 2
end
精彩评论