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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
> 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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
>
>
> On Thu, Jul 14, 2011 at 5:53 AM, Andrew Skegg <andrewskegg-
BUHhN...@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 active
year. 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