有更好的方法吗?即更好地链接命名范围
这段代码工作得很好,但我正在查看它并认为它可以更干净。也许有一种更惯用的 ruby/rails 方法可以做到这一点?顺序很重要,因为 member_of 范围必须放在最后,但在分页之前(返回集合而不是范围)
这样做的一个优点是,很清楚发生了什么
@locations = Location.send(params[:type]) if type_sent_and_valid? #refine to a particular type if present
@locations = (@locations || Location.locatable).near(latlng_params) if latlng_sent? #refine to location
@locations = (@locations || Location).member_of(@interest_group.id).paginate(:page=>params[:page], :per_page=>20)
如果 params 字符串是这样的: 那么
?lat=50&lng=150&type=restaurant&page=1
它应该产生这个
Location.restaurant.near([50.0,150.0]).member_of(@interest_group).paginate(:page=>1, :per_page=>20)
This code works fine bu I'm looking at it and thinking it could be cleaner. Perhaps there is a more idiomatic ruby / rails way of doing this? The order is important because the member_of scope has to come last but before the pagination (which returns a collection and not a scope)
The one advantage of this is that it's pretty clear what's happening
@locations = Location.send(params[:type]) if type_sent_and_valid? #refine to a particular type if present
@locations = (@locations || Location.locatable).near(latlng_params) if latlng_sent? #refine to location
@locations = (@locations || Location).member_of(@interest_group.id).paginate(:page=>params[:page], :per_page=>20)
This if a params string was something like this:
?lat=50&lng=150&type=restaurant&page=1
Then it should yield this
Location.restaurant.near([50.0,150.0]).member_of(@interest_group).paginate(:page=>1, :per_page=>20)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的一种方法是使用滑动范围机制,使用相同的变量一次移动一步:
您可以根据需要添加其他条件。
One way to clean this up is to use a sliding scope mechanism where you move the scope one step at a time using the same variable:
You can add other conditions as required.