ruby-on-railsactiverecordhas-and-belongs-to-many

Rails Habtm with a field


Is that possible to have a field in a has and belongs to many table?

Just like favorite:

create_table :messages_users, :id => false, :force => true do |t|
  t.integer :message_id, :null => false
  t.integer :user_id, :null => false

  t.boolean :favorite, :null => false

  t.timestamps
end

I saw timestamps works well, thanks to ActiveRecord. But when I try to add favorite into the table and then I try:

Message.first.users << User.first

Then I get this error message:

ActiveRecord::StatementInvalid: SQLite3::SQLException: messages_users.favorite may not be NULL: INSERT INTO "messages_users" ("created_at", "message_id", "updated_at", "user_id") VALUES ('''2010-05-27 06:07 :50.721512''', 1, '''2010-05-27 06:07:50.721512''', 1)

I would like to use a habtm, I don't like has_many foo though bar association :)

Is that possible?

Thanks a lot.


Solution

  • No it is not possible.
    Use has_many foo though bar.