Hello. How do I access blog object/model from the following active record class: class Entry < ActiveRecord::Base belongs_to :blog def self.test puts blog.title end end In the example above I want to print the blog title. Thanks for help! -- Posted via http://www.ruby-forum.com/.
On Oct 23, 3:56 pm, Aljaz Fajmut <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello. > > How do I access blog object/model from the following active record > class: > > class Entry < ActiveRecord::Base > > belongs_to :blog > > def self.test > puts blog.title > end > endDon''t make test a class method - individual instances of Entry have an associated blog, not the class as a whole. Fred> > In the example above I want to print the blog title. > > Thanks for help! > -- > Posted viahttp://www.ruby-forum.com/.
Quoting Aljaz Fajmut <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hello. > > How do I access blog object/model from the following active record > class: > > class Entry < ActiveRecord::Base > > belongs_to :blog > > def self.test > puts blog.title > end > end > > In the example above I want to print the blog title. >The class doesn''t have a blog, individual entries have blogs. entry = Entry.find(....whatever, :include => :blog) puts entry.blog.title The :include option "eager loads" the blog at the same time as the entry. Without it, the blog will be loaded when the puts statement executes. HTH, Jeffrey