2

Possible Duplicate:
Model Using Modules in Rails Application

I have a User model that is getting really fat and I want to break it up into submodules that correspond to the different interactions such as a user's interaction with friendships, interaction with events, interaction with weapons, etc. etc. What's the best way to go about it?

The goal is to do something like:

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

This is my attempt thus far: create app/models/user_helper/friendship.rb which has the code as

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

then in the User model,

include ::UserHelper::Friendship

Doesn't work though and I get undefined method when I do "User.first.friends_with?"

What I know WILL work is if I have a random file in the models directory like "UserFriendshipHelper.rb" defining

module UserFriendshipHelper

and include that, but I kinda would like to do it in a more encapsulated, organized way. Any tips or resources much appreciated.

Thanks!

Community
  • 1
  • 1
ambertch
  • 7,581
  • 4
  • 28
  • 40
  • Take a look at this similar question - http://stackoverflow.com/questions/3781307/model-using-modules-in-rails-application/3811664 My answer has a couple of options for modularising fat models which you may find helpful. – Sidane Sep 30 '10 at 19:34
  • O nice, dunno why I couldn't find that question. Thank you very much - if you put a answer w/ that link I can mark yours as the answer – ambertch Sep 30 '10 at 21:32
  • in this scenario, this question should probably be flagged as a duplicate of the other question. – Andrew Grimm Sep 30 '10 at 23:43

0 Answers0