# ActiveRecord Problem

**URL:** <https://www.ruby-forum.com/t/activerecord-problem/120378>\
**Category:** Ruby\
**Created:** [November 16, 2007, 11:23am UTC](https://www.ruby-forum.com/t/activerecord-problem/120378 "2007-11-16T11:23:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 16, 2007, 11:23am UTC](https://www.ruby-forum.com/t/activerecord-problem/120378/1 "2007-11-16T11:23:13Z")

</div>

Hey!

ich habe folgende tabellen in meiner sqlite datenbank:

users ([pk] id, username…)

courses ([pk] id, coursename…)

users\_courses ([pk] users\_id, [pk] courses\_id, score)

ich mÃ¶chte die n:m beziehung zwischen users und courses Ã¼ber  
useres\_course modellieren.  
Dazu hab ich 3 Models angelegt:

class User \< ActiveRecord::Base  
has\_many :users\_courses  
has\_many :courses, :through =\> :users\_courses  
end

class Course \< ActiveRecord::Base  
#set\_table\_name ‘courses’  
has\_many :users\_courses  
has\_many :users, :through =\> :users\_courses  
end

class UserCourse \< ActiveRecord::Base  
belongs\_to :courses  
belongs\_to :users  
end

Ein find z.B. Ã¼ber das User Model funktioniert einwandfrei.

Frage ich aber z.B. sowas wie das hier ab:

u = User.find(:first, :conditions =\> [“username = ?”, session[“user”]])  
usercourses = u.users\_courses.collect{|x| x.courses}

bekomm ich den Fehler: uninitialized constant User::UsersCourse

hat jemand eine Idee?

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 16, 2007, 11:50am UTC](https://www.ruby-forum.com/t/activerecord-problem/120378/2 "2007-11-16T11:50:40Z")

</div>

On Nov 16, 2007 4:23 AM, Christian K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ich möchte die n:m beziehung zwischen users und courses über  
> useres\_course modellieren.  
> has\_many :users, :through =\> :users\_courses
> 
> u = User.find(:first, :conditions =\> [“username = ?”, session[“user”]])  
> usercourses = u.users\_courses.collect{|x| x.courses}
> 
> bekomm ich den Fehler: uninitialized constant User::UsersCourse
> 
> hat jemand eine Idee?

I don’t know any German, but I can guess that your problem is with  
your UserCourse class model. It doesn’t seem like it matches your  
table name. The #set\_table\_name and #set\_primary\_key methods help, if  
you want to match a database. I’ve played around with this quite a  
bit.

I don’t know your exact model either, but maybe…

class UserCourse \< ActiveRecord::Base  
set\_table\_name :users\_courses  
belongs\_to :course  
belongs\_to :user  
end

hth a lit,  
Todd

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 16, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/activerecord-problem/120378/3 "2007-11-16T14:05:40Z")

</div>

Todd B. wrote:

> On Nov 16, 2007 4:23 AM, Christian K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ich mï¿½chte die n:m beziehung zwischen users und courses ï¿½ber  
> > useres\_course modellieren.  
> > has\_many :users, :through =\> :users\_courses
> > 
> > u = User.find(:first, :conditions =\> [“username = ?”, session[“user”]])  
> > usercourses = u.users\_courses.collect{|x| x.courses}
> > 
> > bekomm ich den Fehler: uninitialized constant User::UsersCourse
> > 
> > hat jemand eine Idee?
> 
> I don’t know any German, but I can guess that your problem is with  
> your UserCourse class model. It doesn’t seem like it matches your  
> table name. The #set\_table\_name and #set\_primary\_key methods help, if  
> you want to match a database. I’ve played around with this quite a  
> bit.
> 
> I don’t know your exact model either, but maybe…
> 
> class UserCourse \< ActiveRecord::Base  
> set\_table\_name :users\_courses  
> belongs\_to :course  
> belongs\_to :user  
> end
> 
> hth a lit,  
> Todd

I tried to modify my models:

class UserCourse \< ActiveRecord::Base  
set\_table\_name ‘users\_courses’  
set\_primary\_key ‘users\_Id’  
set\_primary\_key ‘courses\_Id’  
belongs\_to :course  
belongs\_to :user  
end

class User \< ActiveRecord::Base  
set\_table\_name ‘users’  
set\_primary\_key ‘Id’  
has\_many :users\_courses  
has\_many :courses, :through =\> :users\_courses  
end

class Course \< ActiveRecord::Base  
set\_table\_name ‘courses’  
set\_primary\_key ‘Id’  
has\_many :users\_courses  
has\_many :users, :through =\> :users\_courses  
end

The task is to select e.g. all attributes from UserCourse for a certain  
User and Course.  
I keep getting the same error.  
How do a create a many to many realtion with attributes at all in  
activerecord?

thx ck
