unless scope.nil?
@page ||= Page.find(id, :scope => Page.find(scope) )
else
@page ||= Page.find(id)
end
views:
95answers:
5
A:
You could do:
@page ||= unless scope.nil?
Page.find(id, :scope => Page.find(scope))
else
Page.find(id)
end
mipadi
2010-05-07 14:37:08
A:
Or:
@page ||= scope.nil? ? Page.find(id) : Page.find(id, :scope => Page.find(scope))
Dimitri
2010-05-07 14:56:04
+1
A:
I would write the block in question like the following. It really comes down to preference, but I find this way to be the most readable.
@page ||=
if scope
Page.find id, :scope => Page.find(scope)
else
Page.find id
end
Alex
2010-05-07 15:11:03
+2
A:
This is a bit DRYer:
find_opts = scope.nil? ? {} : {:scope => Page.find(scope)}
@page ||= Page.find(id, find_opts)
glenn jackman
2010-05-07 15:36:35
this is the neatest certainly, and as mentioned in the end its down to preference of course.
Globalkeith
2010-05-11 15:38:01