Josh S. wrote:
Federico F. wrote:
Nauhaie wrote:
I would be very interested in an answer, too…
But I suggest you read this:
http://rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html#M000530
And try to work around your problem with the the following option:
:source: Specifies the source association name used by has_many :through
queries. Only use it if the name cannot be inferred from the
association. has_many :subscribers, :through => :subscriptions will look
for either +:subscribers+ or +:subscriber+ on Subscription, unless a
+:source+ is given.
So you can have aclass.capable_teachers and
aclass.actually_teaching_teachers or something like that. Can you tell
me if it works?
Nauhaie
If I understood the documentation right, I could do this:
class Teacher< ActiveRecord::Base
has_many :lessons, :through => :works, :source => :teacher
has_many :classes, :through => :can_teach, :source => teacher
end
class Lesson< ActiveRecord::Base
has_many :teachers, :through => :works, :source => :lesson
has_many :teachers, :through => :can_teach, , :source => :lesson
end
Keeping the classes “CanTeach and Work” without changes.
I will try this and let you know.
I responded to your email about this yesterday but I guess my response
was lost. You must have unique names for associations within a class. If
you reuse the same name, only the last has_many will be available. Just
name them differently. And DON’T use :class or :classes - that will just
bork everything up as “class” is both a keyword and a method on Object
in ruby. You can spell it klass or clazz if you want.
You can also take the approach of having only one join model with an
attribute that indicates the state of the relationship, such as whether
the teacher is qualified to teach the class vs if he is currently
teaching it. I like this way best.
class Teaching < ActiveRecord::Base
belongs_to :teachers
belongs_to :courses
end
class Course < ActiveRecord::Base
has_many :teachings
has_many :teachers, :through => :teachings
end
class Teacher < ActiveRecord::Base
has_many :teachings
has_many :courses, :through => :teachings
has_many :courses_can_teach, :through => :teachings,
:class_name => “Course”, :conditions => “status = ‘can’”
has_many :courses_does_teach, :through => :teachings,
:class_name => “Course”, :conditions => “status = ‘does’”
end
–
Josh S.
http://blog.hasmanythrough.com
Hello Josh! wow… thanks for the help, but let me see if I got it:
has_many :courses_can_teach, :through => :teachings,
:class_name => “Course”, :conditions => “status = ‘can’”
This means that I need a table called “courses_can_teach” with the same
structure of the table “courses”, right? The database will have 2
different tables, but as you use :class_name => “Course” they will be
represented with only one model class called “Course”, am I right?
Then you use :conditions => “status = …” so you can use 1 Join Table
instead of two.
However, even though your suggestion is good, I don’t want one join
table, but two, becase I want the Join Table “teachings” to use a
different set of attributes than the Join Table “capabilities”, and it
would be strange to mix both sets in one Join Table. If I want to use
more than one Join Table I don’t know what to do.
In addition, there is something strange in your solution, because I’ll
be able to do this:
t1 = Teacher.find(:first).courses_can_teach
t2 = Teacher.find(:first).courses_does_teach
and t1 << course, etc…
But what if, given a course, I want to do operations over the teachers
associated to it.
If I do this:
c = Course.find(:first).teacher
what am I going to get? A teacher who can teach that course, or a
teacher who does teach that course?
I will probably have to make the searching methods by hand. Then, I
would say that in rails you have to decide between freedom to do your
database models pretty and efficient, and the benefits of using
ActiveRecords Associations.