📎 Webclip
What are some of the things about Ruby on Rails that most developers don't know?
The page highlights two Rails details that many developers may miss. One is that an ActiveRecord relation can be turned into its underlying SQL with ActiveRecord::Relation#to_sql. The other is a way to define filtered associations without hard-coding a model’s schema details, while still keeping association-specific behavior available.
Reading notes#
ActiveRecord::Relation#to_sqlreturns the SQL for a relation, as shown withUser.where(:first_name => 'Jim').order(:created_at).to_sql.- For a
User#published_postsassociation, the page compares a direct filtering approach with an association declaration that uses:where => published.where_values_hash. - The point of that approach is to avoid repeating
:published => trueand to keep the class from hard-coding the other class’s schema. - The example also says this preserves association-specific features such as eager loading.
- The page frames the second point as keeping the Law of Demeter intact.
