Posts

Showing posts from 2012

rails questions

1) Polymorphic association http://robots.thoughtbot.com/post/159809241/whats-the-deal-with-rails-polymorphic-associations What’s the deal with Rails’ polymorphic associations? The latest big ActiveRecord feature to Rails has been polymorphic associations. Not very clear at first, I found out they’re easier to understand with a couple examples. A common one: class Person < ActiveRecord::Base has_one : address , : as => : addressable end class Company < ActiveRecord::Base has_one : address , : as => : addressable end class Address < ActiveRecord::Base belongs_to : addressable , : polymorphic => true end This basically allows the address class to  belongs_to  any model. Which is nice, the alternative would be to say: class Address < ActiveRecord::Base belongs_to : person belongs_to : company end Then you’d have all these foreign keys in your addresses table but you only would ever have a value for one of them beca