views:

29

answers:

1

Hello,

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.

+1  A: 

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

clyfe
If you want a comparison between habtm and has_many :throughthis 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
Thank you for this answer. I think I will use has_many :through :(
moshimoshi