Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Question:
- How can I get movie details like title using created relations? Should I correct them?
- ## Console
- ernest@ernest-desktop:~/Dokumenty/Projekty/bort$ script/console
- Loading development environment (Rails 2.2.2)
- >> user = User.find(1)
- => #<User id: 1, login: "admin", identity_url: nil, name: "", email: "ernest@bzdury.pl", crypted_password: "683489dbe6a1c66b7a84310223b5ca64f498e7f5", salt: "aebe39509659f8c15383fd9970348017a8dd8e41", remember_token: nil, activation_code: nil, state: "active", last_active: "2008-12-27 03:13:09", remember_token_expires_at: nil, activated_at: "2008-12-27 02:13:10", deleted_at: nil, created_at: "2008-12-27 02:13:10", updated_at: "2009-01-03 21:33:04">
- >> user.favorites
- => [#<Favorite id: 8, fav_id: 58, fav_type: "Movie", user_id: 1, created_at: "2009-01-09 05:40:23", updated_at: "2009-01-09 05:40:23">, #<Favorite id: 9, fav_id: 55, fav_type: "Movie", user_id: 1, created_at: "2009-01-09 09:57:42", updated_at: "2009-01-09 09:57:42">, #<Favorite id: 10, fav_id: 55, fav_type: "Movie", user_id: 1, created_at: "2009-01-09 09:58:42", updated_at: "2009-01-09 09:58:42">, #<Favorite id: 11, fav_id: 55, fav_type: "Movie", user_id: 1, created_at: "2009-01-09 09:59:15", updated_at: "2009-01-09 09:59:15">, #<Favorite id: 12, fav_id: 55, fav_type: "Movie", user_id: 1, created_at: "2009-01-09 10:01:39", updated_at: "2009-01-09 10:01:39">]
- >> user.favorites.movies
- NoMethodError: undefined method `movies' for #<Class:0xa516f08>
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1833:in `method_missing_without_paginate'
- from /home/ernest/Dokumenty/Projekty/bort/vendor/gems/mislav-will_paginate-2.3.6/lib/will_paginate/finder.rb:167:in `method_missing'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in `send'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:370:in `method_missing_without_paginate'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2003:in `with_scope'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in `send'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_proxy.rb:202:in `with_scope'
- from /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/associations/association_collection.rb:366:in `method_missing_without_paginate'
- from /home/ernest/Dokumenty/Projekty/bort/vendor/gems/mislav-will_paginate-2.3.6/lib/will_paginate/finder.rb:167:in `method_missing'
- from (irb):3
- >>
- ## Movie model
- has_many :favorites, :as => :fav
- ## User model
- has_many :favorites
- has_many :movies
- ## Favorites model
- belongs_to :favs, :polymorphic => true
- belongs_to :user
Add Comment
Please, Sign In to add comment