I’m creating my own little task system in Ruby on Rails, but I need some guidance on what to call my attributes, and if I’m thinking about the right solution.
So I have a Task model.
A task can have two relations to a user: The creator and the user that’s responsible for the task.
I can of course just call my attributes: creator:integer, and a responsible:integer, but what is the preferred Ruby on Rails way of doing this?
Should they just be called the above, or creator_user_id:integer or should I make a relation table with a user_id and a task_id and then a role?
In your migration you’ll want the following:
This was generated by:
rails g model task creator:references responsible:referencesIn your database this will create a
taskstable with two columns:creator_idandresponsible_id. By default Ruby on Rails will think that these columns refer to two ActiveRecord models,CreatorandResponsible. However, as you want these to refer to yourUsermodel you’ll need to add the following to yourTaskmodel:In
app/models/tasks.rbThis will tell Ruby on Rails to use the
Usermodel for these relations. So when you do something liketask.creatoryou’ll get back aUsermodel, and the same fortask.responsible.