0

I’m creating a chore tracker app as my first Rails project and I’m wondering if the associations I've created make sense or could be improved. Here are the details of the app:

  • A user makes a chore list and becomes the “owner”(i.e. “admin”) of that list.
  • The owner can create/edit chores for the list. They can also “approve" other users to complete tasks on the list. These users can ONLY complete tasks.
  • The owner, along with the usual admin abilities, can also complete tasks on a list that they own.
  • Owners can own multiple lists. Users can be approved to complete tasks on multiple lists.

And here are the relationships I’ve roughed out that I’m looking for feedback on:

(Model)User

  • has_many :lists
  • has_many :owners, class_name: “List”, foreign_key: “list_id"
  • has_many :chores, through: :lists

(Model)List

  • has_many :users
  • has_many :chores
  • belongs_to :owner, class_name: “User”, foreign_key: “owner_id"

(Model)User_List

  • belongs_to :user
  • belongs_to :list

(Model)Chore

  • belongs_to :list
  • has_many :users, through: :lists

Any red flags? Thank you in advance!

1 Answer 1

0

I would do something like this:

# user.rb
class User
  has_many :lists_users
  has_many :lists, through: :lists_users
  has_many :chores, through :lists
end

# list.rb
class List
  has_many :lists_users
  has_many :users, through: :lists_users
  has_many :chores
end

# lists_user.rb
class ListsUser
  belongs_to :user
  belongs_to :list

  # field (Representational, add it in database)
  :user_role
    - member (can view)
    - collaborator (can mark complete)
    - owner (can do everything)
end

# chore.rb
class Chore
  belongs_to :list
end

NOTE: I changed the association model to ListsUser as per rails naming conventions.

Please let me know if i missed something.

2
  • This makes sense. Just so I can make sure I'm understanding, because a user can belong to many lists, but only one role for that list, I can add the role column to ListUsers instead of User?
    – SaHig
    Dec 6, 2017 at 19:06
  • Yes, that's exactly why i added role column in ListUsers. Dec 7, 2017 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.