On Oct 11, 4:33 pm, Al Rowan <[email protected]> wrote:
> 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

Just :name.  There's no need to repeat your table name in your field
names.

>   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

Again, just :name.

>
>   belongs_to :user
>   belongs_to :unit
>   has_many :marks
>   has_and_belongs_to_many :schemes
>
> end

You have User, Unit, and Mark already defined, right?

>
> 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

This generally looks good so far.  Make sure you install the Foreigner
gem so you can easily put foreign key constraints in your DB.
>
> 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 ()

That's peculiar.  Seems like Rails is not generating the right SQL to
send to the DB.  I gather you're doing this from the Rails console;
have you restarted it since adding the classes and running the
migrations given above?  If not, then do so and see if the problem
persists.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to