0

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.

moshimoshi
  • 251
  • 1
  • 6
  • 15

1 Answers1

1

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
clyfe
  • 23,695
  • 8
  • 85
  • 109
  • If you want a comparison between habtm and has_many :through this is the [right place to start][1]. The blog has many other articles on has_many :through which I encourage you to dig into. Basically clyfe is right and I don't understand why you would prefer habtm when it's not created with the scope you are trying to achieve. Switch to has_many :through and you'll be amazed at how flexible it is once you get to know how it works. [1]: http://blog.hasmanythrough.com/2006/2/28/association-goodness – tommasop May 27 '10 at 08:06
  • Thank you for this answer. I think I will use has_many :through :( – moshimoshi May 28 '10 at 19:24