Todd Benson
11/16/2007 10:50:00 AM
On Nov 16, 2007 4:23 AM, Christian Kerth
<christian.kerth@dynamicmedia.at> wrote:
> 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=F6chte die n:m beziehung zwischen users und courses =FCber
> useres_course modellieren.
> Dazu hab ich 3 Models angelegt:
>
> class User < ActiveRecord::Base
> has_many :users_courses
> has_many :courses, :through =3D> :users_courses
> end
>
> class Course < ActiveRecord::Base
> #set_table_name 'courses'
> has_many :users_courses
> has_many :users, :through =3D> :users_courses
> end
>
> class UserCourse < ActiveRecord::Base
> belongs_to :courses
> belongs_to :users
> end
>
> Ein find z.B. =FCber das User Model funktioniert einwandfrei.
>
> Frage ich aber z.B. sowas wie das hier ab:
>
> u =3D User.find(:first, :conditions =3D> [ "username =3D ?", session["use=
r"]])
> usercourses =3D 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