[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ActiveRecord Problem

Christian Kerth

11/16/2007 10:23:00 AM

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?
--
Posted via http://www.ruby-....

2 Answers

Todd Benson

11/16/2007 10:50:00 AM

0

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

Christian Kerth

11/16/2007 1:06:00 PM

0

Todd Benson wrote:
> On Nov 16, 2007 4:23 AM, Christian Kerth
> <christian.kerth@dynamicmedia.at> 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
--
Posted via http://www.ruby-....