# Noob AR many\_to\_many problem

**URL:** <https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560>\
**Category:** Rails\
**Created:** [January 19, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560 "2008-01-19T16:22:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [January 19, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560/1 "2008-01-19T16:22:39Z")

</div>

I’m receiving the following error …

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:  
id: UPDATE user\_jobs SET user\_id = NULL WHERE (user\_id = 1 AND id IN  
(NULL))

while trying to delete from a join table. Here’s the sequence from the  
console …

> > u = User.find(1)  
> > =\> #\<User:0xb70bc810 @attributes={“name”=\>“Anthony”, “id”=\>“1”}\>
> 
> > j= Job.find(4)  
> > =\> #\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>
> 
> > u.jobs \<\< j  
> > =\> [#\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>]
> 
> > u  
> > =\> #\<User:0xb70bc810 @user\_jobs=[#\<UserJob:0xb7098ec4  
> > @attributes={“job\_id”=\>“4”, “user\_id”=\>“1”}\>, #\<UserJob:0xb709e7d4  
> > @new\_record=false, @attributes={“job\_id”=\>4, “id”=\>1, “user\_id”=\>1},  
> > @errors=#\<ActiveRecord::Errors:0xb709c560 @base=#\<UserJob:0xb709e7d4  
> > …\>, @errors={}\>\>], @attributes={“name”=\>“Anthony”, “id”=\>“1”},  
> > @jobs=[#\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>]\>
> 
> > dj = u.jobs.find(4)  
> > =\> #\<Job:0xb7095c60 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>
> 
> > u.jobs.delete(dj)

Here are my classes  
class Job \< ActiveRecord::Base  
has\_many :user\_jobs  
has\_many :users, :through =\> :user\_jobs  
end

class User \< ActiveRecord::Base  
has\_many :user\_jobs  
has\_many :jobs, :through =\> :user\_jobs  
end

class UserJob \< ActiveRecord::Base  
belongs\_to :job  
belongs\_to :user  
end

Here is my schema  
CREATE TABLE jobs (  
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  
title TEXT NOT NULL  
);  
CREATE TABLE user\_jobs (  
user\_id INTEGER NOT NULL,  
job\_id INTEGER NOT NULL,  
PRIMARY KEY(user\_id,job\_id)  
);  
CREATE TABLE users (  
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  
name TEXT NOT NULL  
);

rails 1.2.4 fron ubuntu.

Any pointers would be great.

thx -ants

Disclaimer: Technically, I’m always wrong!!

* * *

Support the World Aids Awareness campaign this month with Yahoo! for  
Good

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [January 19, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560/2 "2008-01-19T19:26:07Z")

</div>

Solved. Removed ubuntu’s rails and installed 2.0.2 using gems.  
Everything works as it should.

Anthony G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: I’m receiving the  
following error …

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:  
id: UPDATE user\_jobs SET user\_id = NULL WHERE (user\_id = 1 AND id IN  
(NULL))

while trying to delete from a join table. Here’s the sequence from the  
console …

> > u = User.find(1)  
> > =\> #\<User:0xb70bc810 @attributes={“name”=\>“Anthony”, “id”=\>“1”}\>  
> > j= Job.find(4)  
> > =\> #\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>  
> > u.jobs \<\< j  
> > =\> [#\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>]  
> > u  
> > =\> #\<User:0xb70bc810 @user\_jobs=[#\<UserJob:0xb7098ec4  
> > @attributes={“job\_id”=\>“4”, “user\_id”=\>“1”}\>, #\<UserJob:0xb709e7d4  
> > @new\_record=false, @attributes={“job\_id”=\>4, “id”=\>1, “user\_id”=\>1},  
> > @errors=#\<ActiveRecord::Errors:0xb709c560 @base=#\<UserJob:0xb709e7d4  
> > …\>, @errors={}\>\>], @attributes={“name”=\>“Anthony”, “id”=\>“1”},  
> > @jobs=[#\<Job:0xb70aa8f4 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>]\>  
> > dj = u.jobs.find(4)  
> > =\> #\<Job:0xb7095c60 @attributes={“title”=\>“Farmer”, “id”=\>“4”}\>  
> > u.jobs.delete(dj)

Here are my classes  
class Job \< ActiveRecord::Base  
has\_many :user\_jobs  
has\_many :users, :through =\> :user\_jobs  
end

class User \< ActiveRecord::Base  
has\_many :user\_jobs  
has\_many :jobs, :through =\> :user\_jobs  
end

class UserJob \< ActiveRecord::Base  
belongs\_to :job  
belongs\_to :user  
end

Here is my schema  
CREATE TABLE jobs (  
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  
title TEXT NOT NULL  
);  
CREATE TABLE user\_jobs (  
user\_id INTEGER NOT NULL,  
job\_id INTEGER NOT NULL,  
PRIMARY KEY(user\_id,job\_id)  
);  
CREATE TABLE users (  
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,  
name TEXT NOT NULL  
);

rails 1.2.4 fron ubuntu.

Any pointers would be great.

thx -ants

Disclaimer: Technically, I’m always wrong!!

* * *

Support the World Aids Awareness campaign this month with Yahoo! for  
Good

Disclaimer: Technically, I’m always wrong!!

* * *

Sent from Yahoo! - a smarter inbox.

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [January 19, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560/3 "2008-01-19T19:34:29Z")

</div>

On 19 Jan 2008, at 18:25, Anthony G. wrote:

> Solved. Removed ubuntu’s rails and installed 2.0.2 using gems.  
> Everything works as it should.

That may be the case, but this will probably popup later on: with  
has\_many :through, UserJob is a genuine model so should have a  
primary\_key.

Fred

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [January 19, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/noob-ar-many-to-many-problem/126560/4 "2008-01-19T22:46:41Z")

</div>

Noted, thx. I’m only playing around and have now gone back to habtm.  
Also went to rails 1.2.6.

Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

On 19 Jan 2008, at 18:25, Anthony G. wrote:

> Solved. Removed ubuntu’s rails and installed 2.0.2 using gems.  
> Everything works as it should.

That may be the case, but this will probably popup later on: with  
has\_many :through, UserJob is a genuine model so should have a  
primary\_key.

Fred

> > > u = User.find(1)  
> > > 0xb709e7d4 …\>, @errors={}\>\>], @attributes={“name”=\>“Anthony”,  
> > > end
> 
> CREATE TABLE users (
> 
> Disclaimer: Technically, I’m always wrong!!  
> Support the World Aids Awareness campaign this month with Yahoo! for  
> Good  
> Sent from Yahoo! - a smarter inbox
> 
> >

Disclaimer: Technically, I’m always wrong!!

* * *

Support the World Aids Awareness campaign this month with Yahoo! for  
Good
