Created
June 10, 2010 20:42
-
-
Save bushymark/433602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##### I need to be able to eager load the notes element on the lesson element. Figured the best way to do it was through an | |
| ##### association. | |
| ############## | |
| # doesn't work . . . conditions on associations don't take lambda's | |
| class Note < ActiveRecord::Base | |
| belongs_to :notable, :polymorphic => true | |
| end | |
| class Lesson < ActiveRecord::Base | |
| has_one :note, :as => :notable, :conditions => lamda {['user_id = ?', User.current.id]} | |
| end | |
| ############## | |
| # Next step was to try default scope, doesn't work, no lambdas to default scope | |
| class Note < ActiveRecord::Base | |
| belongs_to :notable, :polymorphic => true | |
| default_scope lambda {{:user_id => User.current.id}} | |
| end | |
| ############## | |
| # Okay, lets try this manually in my controllers and I will just create some magic model methods: | |
| class Note < ActiveRecord::Base | |
| belongs_to :notable, :polymorphic => true | |
| end | |
| class Lesson < ActiveRecord::Base | |
| has_many :notes, :as => :notable | |
| belongs_to :course | |
| end | |
| class Course < ActiveRecord::Base | |
| has_many :lessons | |
| end | |
| #=> in my controller | |
| course = Course.find params[:course_id] | |
| lessons = course.lessons.find(:all, :include => [:other_stuff, :notes], :conditions => ["notes.user_id = ?", User.current.id]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment