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?
On Nov 16, 2007 4:23 AM, Christian K.
[email protected] 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
Todd B. wrote:
On Nov 16, 2007 4:23 AM, Christian K.
[email protected] 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