【发布时间】:2011-08-24 12:29:09
【问题描述】:
我有将用户送上月球的基本功能:
#Action in a controller
def outer_space
user = User.find(params[:id])
user.board_rocket_to_the_moon
end
#user model
def board_rocket_to_the_moon
#put on space suit, climb in rocket, etc.
end
现在,我想补充一点,只将喜欢旅行的用户送上月球。
将if语句放在控制器还是模型中更好,为什么?
#option 1: Put an if in the controller
def outer_space
user = User.find(params[:id])
user.board_rocket_to_the_moon if user.likes_to_travel
end
#option 2: Stick the if in the user model
def board_rocket_to_the_moon
if self.likes_to_travel
#put on space suit, climb in rocket, etc.
return "BLAST OFF"
else
return "There is no way THIS dude is getting on THAT ship."
end
end
【问题讨论】:
标签: ruby-on-rails model-view-controller