Brian
Brian

Reputation: 61

Proper structure for a join table in Rails 3.2

I am rewriting a PHP based support ticket system in Rails and have run into a snag. I have my users table created and my tickets table created

create_table "tickets", :force => true do |t|
  t.integer  "user_id",       :null => false
  t.integer  "department_id", :null => false
  t.integer  "upload_id",     :null => false
  t.string   "subject",       :null => false
  t.text     "body",          :null => false
  t.string   "status_id",     :null => false
  t.text     "url",           :null => false
  t.datetime "created_at",    :null => false
  t.datetime "updated_at",    :null => false
end

create_table "users", :force => true do |t|
  t.string   "fName",      :null => false
  t.string   "lName",      :null => false
  t.string   "seKey",      :null => false
  t.boolean  "isAdmin"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
  t.string   "auth_token"
end

Everything is working great, I am able to create tickets, etc... Now I am needing to assign 1 or more admins to a ticket and am not sure if I should use a has_and_belongs_to_many relationship or a has_many :through relationship.

The way it is currently setup in PHP is just using a join table that matches a userID with a ticketID. I don't think I will ever need any other data relating to the relationship other than that so is has_and_belongs_to_many the best option?

Also, will this cause an issue since the ticket is already associated with a record in the user table (the original creator)? Basically a ticket will have multiple connections to the users table, one will be the person who submitted it and the rest are users who are assigned to handle it.

Upvotes: 0

Views: 218

Answers (3)

Sam Baumgarten
Sam Baumgarten

Reputation: 2249

Both work; however, personally, I prefer a has_many :through relationship. The reason being is you have access to the join table. To do this generate a model called ticket_user with a field called user_id and ticket_id. Then in the model add

belongs_to :user
belongs_to :ticket

then you can add to the ticket model

has_many :ticket_users
has_many :users :through ticket_user

and this to the user model

has_many :ticket_users
has_many :ticket :through ticket_user

Then to retrieve all of a users tickets do

user.tickets

To get all of a tickets users do

ticket.users

For more info check out this guide

Upvotes: 2

Loken Makwana
Loken Makwana

Reputation: 3848

In your case has_and_belongs_to_many is the extra relationship.

has_and_belongs_to_many :members, :class_name => "User", :join_table => "members_tickets"

You can have to create one table members_users with ticket_id and member_id as fields as shown in http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

now you can get list of assigned members like below

@ticket.members # list of members
@ticket.members << @user # add new member

Upvotes: 0

Rubyman
Rubyman

Reputation: 874

this may work - create two relationships between user and ticket, ticket belongs_to creator, :class_name user and ticket has_many assigned_users, class_name user and in user model has_many created_tickets and has_many assigned_tickets

Upvotes: 0

Related Questions