如何将rails模型分解为模块?

时间:2010-09-30 19:16:48

标签: ruby-on-rails ruby

  

可能重复:
  Model Using Modules in Rails Application

我有一个非常胖的用户模型我希望将其分解为与不同交互相对应的子模块,例如用户与友谊的交互,与事件的交互,与武器的交互等等。什么是最佳方式去做吧?

目标是做一些事情:

u = User.find(1)
friend = User.find(2)
u.friends_with? friend

这是我迄今为止的尝试:创建app / models / user_helper / friendship.rb,其代码为

module UserHelper
  module Friendship
    def friends_with? friend
      ::Friendship.are_friends(self, friend) #this should point to the top-level Friendship class
    end
    ....code code code code....
  end
end

然后在用户模型中

include ::UserHelper::Friendship

虽然不起作用,但当我执行“User.first.friends_with?”时,我得到了未定义的方法。

我所知道的是,如果我在models目录中有一个随机文件,例如“UserFriendshipHelper.rb”定义

module UserFriendshipHelper

并包括这一点,但我有点想以更封装,更有条理的方式来做。任何提示或资源都非常赞赏。

谢谢!

0 个答案:

没有答案