开发者

Rails -- whats the proper relationship model

I've got a rails application built for schools. The users in the system are parents who track their childrens’ activities (aka family members or just members). Teachers are also users and can log in to participate in activities. The Memb开发者_StackOverflow社区er model is the place that the application uses to find participants. So that teachers can be found they are added to the Member model and for the same reason, parents are added as well.

A teacher added to this application would have one record in each table user, user_member, and member. A record in user to allow him to login, 1 record in member so he can be searchable, and 1 in user_member to make the association. In this case finding a user’s own member record is trivial. But if I were a parent with a login and 2 children in the system, James and Brian, one would find 3 records in the Members table, one for James, one for Brian and one for myself. I need to know which member record is mine, not James or Brian.

What’s the best way to model this? I’ve considered two options 1) there’s a foreign key in the user table that points to a user’s own member_id OR 2) the user_members table has a boolean called ‘own_member’ which indicates this user_id’s member_id is the member record for the user. Is there one that would be preferable? One more rails like? And how would I make the call build/create the associations?

The current relationships are modeled like this:

class User < ActiveRecord::Base
  has_many :user_members
  has_many :members, :through => :user_members
end

class UserMember < ActiveRecord::Base
  belongs_to :user
  belongs_to :member
end

class Member < ActiveRecord::Base
  has_many :user_members
  has_many :user, :through => :user_members  
end


It sounds like the UserMember join model describes which members a user has privileges to access. This is a different concept than tracking which Member record is associated to a particular user. It sounds like a user would have a member record, and a member would belong to a user implying the following database structure:

class User < ActiveRecord::Base
  has_many :user_members
  has_many :members, :through => :user_members
  has_one :member
end

class UserMember < ActiveRecord::Base
  belongs_to :user
  belongs_to :member
end

class Member < ActiveRecord::Base
  has_many :user_members
  has_many :user, :through => :user_members 
  belongs_to :user 
end

This means adding another database column to the members table for "user_id". Creating and building the member record from a User instance could be done as follows:

user = User.new
user.build_member
user.create_member

More info on building through associations here: http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

You might consider creating callbacks to automatically create a member record on creation of a user record so you can skip manually building the associated record. An example would be:

# app/models/user.rb
class User < ActiveRecord::Base
  before_create :build_member

  def build_member
    self.build_member
  end
end

More information on callbacks here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜