Given a school model and a student model with the school having a has_many relation to student: has_many :students, :conditions => proc { "year_id=#{send(:active_year_id)}" } where active_year_id is a method defined in the school model, I''m encountering an error that "active_year_id is undefined" when calling: School.where(:active => true).includes(:students) The condition works fine when I do, say, School.where(:id => 10).students. Only when I try to use includes do I get that error. Is that the right behavior. If not, what am I doing wrong and how do I fix ? Using Rails 3.0.9, REE 1.8.7. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9rsY1unEtsEJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 Jul 2011, at 20:23, Vijay Dev <vijaydev.cse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Given a school model and a student model with the school having a has_many relation to student: > > has_many :students, :conditions => proc { "year_id=#{send(:active_year_id)}" } > > where active_year_id is a method defined in the school model, I''m encountering an error that "active_year_id is undefined" when calling: > > School.where(:active => true).includes(:students) > > The condition works fine when I do, say, School.where(:id => 10).students. > > Only when I try to use includes do I get that error. Is that the right behavior. If not, what am I doing wrong and how do I fix ?Interpolated conditions on associations have never worked with includes, since the point would be to load all students in one go which you can''t do in general if each one has different conditions. For join based includes I believe this is flat out impossible. For the other form you might be able to crowbar it in, but it feels very tricky Fred> > Using Rails 3.0.9, REE 1.8.7. > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9rsY1unEtsEJ. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Vijay Dev <vijaydev.cse@...> writes:> > > Given a school model and a student model with the school having a has_manyrelation to student:> > has_many :students, :conditions => proc { "year_id=#{send(:active_year_id)}" } That smells like it belongs in a named scope on the School rather than on the relationship. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Vijayakumar D
2011-Jul-14 06:38 UTC
Re: Re: Including associations with dynamic conditions
On Thu, Jul 14, 2011 at 5:53 AM, Andrew Skegg <andrewskegg-BUHhN+a2lJ4@public.gmane.org> wrote:> Vijay Dev <vijaydev.cse@...> writes: > > > > > > > Given a school model and a student model with the school having a > has_many > relation to student: > > > > has_many :students, :conditions => proc { "year_id=#{send > (:active_year_id)}" } > > That smells like it belongs in a named scope on the School rather than on > the > relationship. > >I want school.students to always return students belonging to the active year. I chose this way so that I don''t need to change too much of code.> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think you can do this: School.includes(:students).where({ :school => {:active => true, :id => 10}) let me know if it work. Ajit -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Vijayakumar D <vijaydev.cse@...> writes:> > > On Thu, Jul 14, 2011 at 5:53 AM, Andrew Skegg <andrewskegg-BUHhN+a2lJ4-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org> wrote:> > Vijay Dev <vijaydev.cse <at> ...> writes: > > > > > > Given a school model and a student model with the school having a has_many > relation to student: > > > > has_many :students, :conditions => proc { "year_id=#{send > (:active_year_id)}" } > That smells like it belongs in a named scope on the School rather than on the > relationship. > > > I want school.students to always return students belonging to the activeyear. I chose this way so that I don''t need to change too much of code. If my ESP is working, given: School << ActiveRecord::Base has_many :students belongs_to :year end Student << ActiveRecord::Base belongs_to :school belongs_to :year end Year << ActiveRecord::Base has_many :schools has_many :students end then finding students belonging to a particular year should be easy: School.first.year.students or closer to your example: School.find(10).year.students -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.