FreeMIS model layer contains, amongst other things: class PupilSet < ActiveRecord::Base has_and_belongs_to_many :users, :join_table=>"set_teacher_link", :conditions=>"end_date IS NULL" belongs_to :school_session # ie 05/06 ............. class User < ActiveRecord::Base has_and_belongs_to_many :pupil_sets, :join_table=>"set_teacher_link", :conditions=>"end_date IS NULL AND school_session_id=''#{SchoolSession.current.id}''", :order=>"set_code" .................... class SchoolSession < ActiveRecord::Base has_many :pupil_sets def SchoolSession.current(time_at=Time.now) SchoolSession.find(:first, :conditions=>[ "session_start_date <= ? AND session_end_date> ?", time_at.to_i, time_at.to_i ]) end ............ This doesn''t work! I think it must have something to do with the fact that ActiveRecord is trying to piece together the relationships first, and is trying to find out what SchoolSession.current is, before it has got to the declaration of that method. It''s the pupil_set<=>school_session relationship that seems to upset it. It works fine if I do this instead: class SchoolSession < ActiveRecord::Base def SchoolSession.current(time_at=Time.now) SchoolSession.find(:first, :conditions=>[ "session_start_date <= ? AND session_end_date> ?", time_at.to_i, time_at.to_i ]) end def pupil_sets PupilSet.find(:all, :conditions=>["school_session_id=?",SchoolSession.current.id], :order=>"set_code") end end I wish I understood this fully :( -- Robert Jones
Robert Jones wrote:> FreeMIS model layer contains, amongst other things: > > class PupilSet < ActiveRecord::Base > > has_and_belongs_to_many :users, > :join_table=>"set_teacher_link", > :conditions=>"end_date IS NULL" > > belongs_to :school_session # ie 05/06 > ............. > > class User < ActiveRecord::Base > > has_and_belongs_to_many :pupil_sets, > :join_table=>"set_teacher_link", > :conditions=>"end_date IS NULL > AND > school_session_id=''#{SchoolSession.current.id}''", > :order=>"set_code" > > .................... > > class SchoolSession < ActiveRecord::Base > has_many :pupil_sets > > def SchoolSession.current(time_at=Time.now) > SchoolSession.find(:first, > :conditions=>[ "session_start_date <= ? > AND session_end_date> ?", > time_at.to_i, time_at.to_i ]) > end > > ............ > > This doesn''t work! I think it must have something to do with the fact > that ActiveRecord is trying to piece together the relationships first, and > is trying to find out what SchoolSession.current is, before it has got to > the declaration of that method. It''s the pupil_set<=>school_session > relationship that seems to upset it. > > It works fine if I do this instead: > > class SchoolSession < ActiveRecord::Base > > def SchoolSession.current(time_at=Time.now) > SchoolSession.find(:first, > :conditions=>[ "session_start_date <= ? > AND session_end_date> ?", > time_at.to_i, time_at.to_i ]) > end > > def pupil_sets > PupilSet.find(:all, > :conditions=>["school_session_id=?",SchoolSession.current.id], > :order=>"set_code") > end > end > > I wish I understood this fully :( >oops - I meant> def pupil_sets > PupilSet.find(:all, > :conditions=>["school_session_id=?",self.id], > :order=>"set_code") > end-- Robert Jones
On Oct 24, 2005, at 11:05 AM, Robert Jones wrote:> FreeMIS model layer contains, amongst other things: > > class PupilSet < ActiveRecord::Base > > has_and_belongs_to_many :users, > :join_table=>"set_teacher_link", > :conditions=>"end_date IS NULL" > > belongs_to :school_session # ie 05/06 > ............. > > class User < ActiveRecord::Base > > has_and_belongs_to_many :pupil_sets, > :join_table=>"set_teacher_link", > :conditions=>"end_date IS NULL > AND school_session_id=''# > {SchoolSession.current.id}''", > :order=>"set_code" > > .................... > > class SchoolSession < ActiveRecord::Base > has_many :pupil_sets > > def SchoolSession.current(time_at=Time.now) > SchoolSession.find(:first, > :conditions=>[ "session_start_date <= ? > AND session_end_date> ?", > time_at.to_i, time_at.to_i ]) > end > > ............ > > This doesn''t work! I think it must have something to do with the > fact that > ActiveRecord is trying to piece together the relationships first, > and is > trying to find out what SchoolSession.current is, before it has got > to the > declaration of that method. It''s the pupil_set<=>school_session > relationship that seems to upset it. > > It works fine if I do this instead: > > class SchoolSession < ActiveRecord::Base > > def SchoolSession.current(time_at=Time.now) > SchoolSession.find(:first, > :conditions=>[ "session_start_date <= ? > AND session_end_date> ?", > time_at.to_i, time_at.to_i ]) > end > > def pupil_sets > PupilSet.find(:all, > :conditions=> > ["school_session_id=?",SchoolSession.current.id], > :order=>"set_code") > end > end > > I wish I understood this fully :(It is because the string in the habtm declaration is being evaluated as the file is read (while the class is being constructed) and pieces are probably missing. The association macros are really only appropriate when all of the conditions are known in advance (although you can make it work otherwise). My recommendation would be to use the explicit "pupil_sets" method, though you ought to know that if you do it that way, you won''t be able to use the method to (for instance) create new pupil sets, i.e.: SchoolSession.pupil_sets.create(...) - Jamis
Jamis Buck wrote:> On Oct 24, 2005, at 11:05 AM, Robert Jones wrote: > >> FreeMIS model layer contains, amongst other things: >> >> class PupilSet < ActiveRecord::Base >> >> has_and_belongs_to_many :users, >> :join_table=>"set_teacher_link", >> :conditions=>"end_date IS NULL" >> >> belongs_to :school_session # ie 05/06 >> ............. >> >> class User < ActiveRecord::Base >> >> has_and_belongs_to_many :pupil_sets, >> :join_table=>"set_teacher_link", >> :conditions=>"end_date IS NULL >> AND school_session_id=''# >> {SchoolSession.current.id}''", >> :order=>"set_code" >> >> .................... >> >> class SchoolSession < ActiveRecord::Base >> has_many :pupil_sets >> >> def SchoolSession.current(time_at=Time.now) >> SchoolSession.find(:first, >> :conditions=>[ "session_start_date <= ? >> AND session_end_date> ?", >> time_at.to_i, time_at.to_i ]) >> end >> >> ............ >> >> This doesn''t work! I think it must have something to do with the >> fact that >> ActiveRecord is trying to piece together the relationships first, >> and is >> trying to find out what SchoolSession.current is, before it has got >> to the >> declaration of that method. It''s the pupil_set<=>school_session >> relationship that seems to upset it. >> >> It works fine if I do this instead: >> >> class SchoolSession < ActiveRecord::Base >> >> def SchoolSession.current(time_at=Time.now) >> SchoolSession.find(:first, >> :conditions=>[ "session_start_date <= ? >> AND session_end_date> ?", >> time_at.to_i, time_at.to_i ]) >> end >> >> def pupil_sets >> PupilSet.find(:all, >> :conditions=> >> ["school_session_id=?",SchoolSession.current.id], >> :order=>"set_code") >> end >> end >> >> I wish I understood this fully :( > > It is because the string in the habtm declaration is being evaluated > as the file is read (while the class is being constructed) and pieces > are probably missing. The association macros are really only > appropriate when all of the conditions are known in advance (although > you can make it work otherwise). My recommendation would be to use > the explicit "pupil_sets" method, though you ought to know that if > you do it that way, you won''t be able to use the method to (for > instance) create new pupil sets, i.e.: > > SchoolSession.pupil_sets.create(...) > > - JamisThat makes sense - thanks. I can live without SchoolSession.pupil_sets.create working :) -- Robert Jones