Ruby on Rails:干燥初始化一些变量的重复代码块
我有一个重复的代码块,它在一堆不同的控制器方法中初始化一些变量。有没有办法让我使用模型方法进行 DRY,而不是在每个控制器方法中重复相同的代码块?
基本上,它用于社交网站,它会提取用户的朋友列表,然后根据用户拥有的存储在友谊模型中的权限构建朋友桶。这种桶的重复初始化正是我试图做到的 DRY。
通常我会使用模型方法,但在这种情况下,3 个单独的变量将基于一次数据库拉取进行初始化,而且调用频率足够高,我不想通过访问数据库 3 次来使其不必要的低效。在 C 中,我只使用作为变量传入的指针。
事情是这样的:
def example_method
friendships = @user.friendships
view_permission_friends = []
write_permission_friends = []
message_permission_friends = []
for friendship in friendships
if friendship.view_permission then view_permission_friends << friendship.friend_id end
if friendship.write_permission then write_permission_friends << friendship.friend_id end
if friendship.message_permission then message_permission_friends << friendship.friend_id end
end
#Do something with the 3 initialized arrays here
end
I've got a repeated code block that initializes a few variables in a bunch of different controller methods. Is there a way for me to make this DRY with a model method as opposed to repeating the same code block in each controller method?
Basically, it's for a social site, and it's pulling up a user's list of friends, and then building buckets of friends based on the permissions the user has which are stored in a friendship model. This repeated initialization of buckets is what I'm trying to make DRY.
Normally I would use a model method, but in this case, 3 separate variables are being initialized based on one database pull, and this is called often enough I don't want to make it unnecessarily inefficient by hitting the database 3 times. In C, I would just use pointers passed in as variables.
It goes something like this:
def example_method
friendships = @user.friendships
view_permission_friends = []
write_permission_friends = []
message_permission_friends = []
for friendship in friendships
if friendship.view_permission then view_permission_friends << friendship.friend_id end
if friendship.write_permission then write_permission_friends << friendship.friend_id end
if friendship.message_permission then message_permission_friends << friendship.friend_id end
end
#Do something with the 3 initialized arrays here
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想了一下,我认为这段代码应该进入你的用户模型。 (或者上面示例中的 @user 类。)两个原因:
依赖于 Rails 内部查询缓存的快速而简单的方法如下所示。添加到用户模型中:
然后您的控制器只需执行以下操作:(
注意 - 通过延迟缓存和参数化权限,这里有更多的优化空间和更好的灵活性。)
I thought about it for a bit, and I think this code should go into your User model. (Or whatever class @user is in your example above.) Two reasons:
The quick and easy way which relies on Rails' internal query caching would look like this. Added into the User model:
Your controllers then simply do this:
(Note - there's more room for optimization and better flexibility here via lazy caching and parameterizing the permission.)
怎么样...
这给你一个带有键的散列而不是单独的数组,但应该足够了。
How about...
This gives you a hash with keys instead of individual arrays, but should suffice.
使用
Enumerable#inject
,Enumerable#find_all
和Object#instance_variable_set
:Use
Enumerable#inject
,Enumerable#find_all
andObject#instance_variable_set
: