Rails where condition with nil value Ask Question. Asked 10 years, 4 months ago. Active 9 months ago. Viewed 48k times.
What is the best practise to use in rails? To solve this problem. I find two solutions Solution 1 if params[:id]. Improve this question. Swe Swe 1 1 gold badge 3 3 silver badges 9 9 bronze badges. Sorry, but your question doesn't make any sense to me anymore. The two solutions you mention above do two completely different things. My answer and that of leenasn explain why your "Solution 2" does not work. What do you want? Yes you are right. There are completely 2 different things.
I thought they are same. Solution 1 does not work for me because parameter could be nil and it will find records with NULL value. What I want is if parameter is nil, don't get anything. Thanks for your answer. It makes me clear after reading all answers. Thank you guys — Swe. Add a comment. Active Oldest Votes. However, if it's a parameter and you don't know whether it will be nil or not, you'd better use: MyTable. Improve this answer.
Mischa Mischa Note that the above only applies to nil. I didn't, of course In rails 4 you can specify the value as null using the new hash syntax. I wish Rails had a Laravel-esque interface like MyTable. As mentioned in the below comment, you can use MyTable. I hear you and it works just fine, but just hoping for an alternative interface someday. ActiveRecord will cope with this very well, even when null is included in an array.
David Aldridge David Aldridge I suppose it's so called "rails magic" you can pass ranges Client. This will return true if objectname is nil. You should not use dot operator or a nil value, else it will throw. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How I can check if an object is null in ruby on rails 2? Ask Question. Asked 9 years, 6 months ago. Active 11 months ago. Viewed k times. Improve this question. George Panayi George Panayi 1, 6 6 gold badges 26 26 silver badges 39 39 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Pavel Veller Pavel Veller 5, 1 1 gold badge 23 23 silver badges 24 24 bronze badges.
It would also be advisable to play with rails console to try out statements on the fly — Jonathan. You might also want to explain a bit more about how you populate the reviews if it is in fact. Show 1 more comment. You can check if an object is nil null by calling present? And then , while uncommon, is fine in Ruby if statements. Should also be noted that present?
Chloe Chloe 22k 37 37 gold badges silver badges bronze badges. Joshua Cheek Joshua Cheek You can use the simple not flag to validate that. Example if! Vasanth Saminathan Vasanth Saminathan 4 4 bronze badges.
0コメント