trying to set up a “has_and_belongs_to_many” relationship
would very much appreciate the help, not sure what im doing wrong at all.
Scheme.rb
class Scheme < ActiveRecord::Base
validates :schemename, :presence => true
belongs_to :user
has_many :levels, :dependent => :destroy
has_and_belongs_to_many :works
end
Work.rb
class Work < ActiveRecord::Base
validates :workname, :presence => true
belongs_to :user
belongs_to :unit
has_many :marks
has_and_belongs_to_many :schemes
end
migration for schemes_works
class CreateSchemesWorks < ActiveRecord::Migration
def self.up
create_table :schemes_works, :id => false do |t|
t.references :scheme
t.references :work
end
THE ERROR
scheme = Scheme.last
=> #<Scheme id: 21, schemename: “another”, colour: nil, share: nil, subject_id: nil, user_id: 2, created_at: “2010-10-05 08:17:03”, updated_at: “2010-10-05 08:17:03”>work = Work.last
=> #<Work id: 5, workname: “thrd”, unit_id: 4, user_id: nil, unit: nil, created_at: “2010-10-11 17:24:51”, updated_at: “2010-10-11 17:24:51”>scheme.works << work
ActiveRecord::StatementInvalid: SQLite3::SQLException: near “)”: syntax error: INSERT INTO “schemes_works” () VALUES ()
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:202:inlog' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in
execute’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:263:ininsert_sql' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/sqlite_adapter.rb:149:in
insert_sql’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:44:ininsert' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:16:in
insert’
from /Library/Ruby/Gems/1.8/gems/arel-1.0.1/lib/arel/engines/sql/engine.rb:30:increate' from /Library/Ruby/Gems/1.8/gems/arel-1.0.1/lib/arel/algebra/relations/writes.rb:24:in
call’
from /Library/Ruby/Gems/1.8/gems/arel-1.0.1/lib/arel/session.rb:17:increate' from /Library/Ruby/Gems/1.8/gems/arel-1.0.1/lib/arel/algebra/relations/relation.rb:159:in
insert’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:70:ininsert_record' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:136:in
<<’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:479:inadd_record_to_target_with_callbacks' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:135:in
<<’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:133:ineach' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:133:in
<<’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:158:intransaction' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in
transaction’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/transactions.rb:204:intransaction' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:157:in
transaction’
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:132:in `<<’
from (irb):4>>