Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created December 13, 2011 01:29
Show Gist options
  • Save patrick99e99/1469966 to your computer and use it in GitHub Desktop.
Save patrick99e99/1469966 to your computer and use it in GitHub Desktop.
ree-1.8.7-2011.03 :020 > c
=> #<Comment id: 3, body: "foo!", commentable_id: 111, commentable_type: "Post", user_id: 18, created_at: "2011-07-13 15:25:49", updated_at: "2011-07-27 18:19:04", helpful_at: "2011-07-27 18:19:04", research_report_id: nil>
ree-1.8.7-2011.03 :021 > c.commentable.class
=> Post(id: integer, title: string, method_of_exchange: string, you_tube_url: string, lat: decimal, lng: decimal, bounty: integer, distance: integer, user_id: integer, consider_similar_offers: boolean, description: text, created_at: datetime, updated_at: datetime, address: string, city: string, state: string, zip: string, country: string, county: string, category_id: integer, slug: string, status: string, popularity: integer, delta: boolean, share_count: integer, needs_comment: decimal, needs_offer: decimal, district: string, concierge_flag: string, request_details: text)
ree-1.8.7-2011.03 :023 > c.commentable.blah
NoMethodError: undefined method `blah' for "#<Post:0x1058e57f0>":Post
from /Users/patrick/.rvm/gems/ree-1.8.7-2011.03@foo/gems/activerecord-3.0.7/lib/active_record/associations/association_proxy.rb:216:in `method_missing'
from (irb):23
ree-1.8.7-2011.03 :024 > c.commentable.id
=> 111
ree-1.8.7-2011.03 :025 > p = Post.find(111)
=> #<Post id: 111, title: "foobar", created_at: "2011-07-02 20:59:05", updated_at: "2011-11-09 21:00:19"
>
ree-1.8.7-2011.03 :026 > p.blah
=> "blah!"
class Post < ActiveRecord::Base
post.has_many :comments, :as => :commentable
def method_missing(name, *args, &block)
puts "blah!"
end
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment