Here is an odd one, and I have made up a test case for it. After
creating or updating a record that has a habtm relationship, and selecting one of the HABTM-dervied checkboxes, it attempts to INSERT a empty record into the join table.
I'm using a modified version of the books and authors classes as an
example. I have purposefully used different conventions, as it appears to be the source of the issue. The schema I'm trying to use AR on doesn't match RoR convention 100%, but it should work.
book.rb:
class Book < ActiveRecord::Base
has_and_belongs_to_many :authors, :class_name => "Author",
:table_name => 'authors' , :join_table=>'books_join',
:foreign_key => :booksId, :association_foreign_key => :authorsId
def to_s
"#{typename} | #{description}"
end
end
author.rb:
class Author < ActiveRecord::Base
has_and_belongs_to_many :books, :class_name => "Book", :table_name => 'books',
:join_table=>'books_join', :foreign_key => :authorsId,
:association_foreign_key => :booksId
def to_s
"#{typename} | #{description}"
end
end
books_controller.rb:
class BooksController < ApplicationController
layout "user"
active_scaffold :books do |c|
c.columns[:authors].form_ui = :select
end
end
001_create_books.rb:
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.column :typename, :string, :null => false
t.column :description, :string, :null => false
end
end
def self.down
drop_table :books
end
end
002_create_authors.rb:
class CreateAuthors < ActiveRecord::Migration
def self.up
create_table :authors do |t|
t.column :typename, :string, :null => false
t.column :description, :string, :null => false
end
end
def self.down
drop_table :authors
end
end
003_create_books_join.rb:
class CreateBooksJoin < ActiveRecord::Migration
def self.up
create_table :books_join, :id => false do |t|
t.column :booksId, :integer
t.column :authorsId, :integer
end
end
def self.down
drop_table :books_join
end
end
The log:
SQL (0.000000) BEGIN
SQL (0.000000) INSERT INTO books (`description`, `typename`) VALUES('Test Description', 'Test Book')
SQL (0.050000) COMMIT
SQL (0.000000) BEGIN
SQL (0.000000) INSERT INTO authors (`description`, `typename`) VALUES('Test Author', 'Test Author')
SQL (0.050000) COMMIT
Join Table Columns (0.000000) SHOW FIELDS FROM books_join
Author Load (0.000000) SELECT * FROM authors INNER JOIN books_join ON authors.id = books_join.authorsId WHERE (books_join.booksId = 4 )
SQL (0.000000) BEGIN
books_join Columns (0.000000) SHOW FIELDS FROM books_join
SQL (0.000000) INSERT INTO books_join () VALUES ()
SQL (0.050000) COMMIT