0

I have two models:

USERS
has_many :celebrations
has_many :boards, :through => :celebrations

BOARDS
has_many :celebrations
has_many :users, :through => :celebrations


CELEBRATIONS
:belongs_to :user
:belongs_to :board

I understand that I create a third join table and model called "Celebrations" which does not require an ID.

   create_table :, :id => false do |t|
      t.column :board_id,        :int, :null => false
      t.column :user_id,         :int, :null => false 
      t.column :role,            :string, :null => false
      t.column :token,           :string
      t.timestamps
      end
  end

How do I access the information?

user.celebrations.role user.celeberations.token

user.boards board.users

Thanks in advance. I understand its a real newbie question.

chell
  • 7,646
  • 16
  • 74
  • 140
  • For `has_many :through`, you need to have `id` (primary key) (For HABTM, you need not have.). To access the information, what you have defined is correct(`user.celebrations.role`, `user.celeberations.token`, `user.boards`, `board.users`). Remove `:id => false` from the migration. You can also say `t.string :role` instead of `t.column :role, :string` in the migration. – rubyprince Mar 07 '11 at 09:18
  • Thank you I will do all that you have suggested. Thanks for your response. It really helps – chell Mar 07 '11 at 09:35

1 Answers1

1

Yes, you can but if the join table has additional attributes then you should convert it to the full model. I mean to create a new Rails model with id, article_id, author_id and additional fields like role.

This is the Rails way of implementing such things. There is a small overhead of making the join table a little bit bigger. However with full join model it is possible to use standard Rails functions to create and update that model.

As far I remember has_many :through option was added to support better join models.

Greg Dan
  • 6,198
  • 3
  • 33
  • 53
  • Thanks Greg. I was not sure about taking the ID off of the join table when I wrote it. Now I understand that I need it. Thanks for your help. – chell Mar 07 '11 at 09:36