Rails Habtm with a field
Posted
by moshimoshi
on Stack Overflow
See other posts from Stack Overflow
or by moshimoshi
Published on 2010-05-27T06:24:50Z
Indexed on
2010/05/27
6:31 UTC
Read the original article
Hit count: 283
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.
© Stack Overflow or respective owner