0
votes

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

1 Answers

0
votes

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.