I’ve been struggling with rails models a bit and could use some guidance. I have two models: Package and Package_item. Packages have many package_items associated to them and I’m trying to figure out how to return the package information along with the array of packge_items in a single method.
class Package < ActiveRecord::Base
has_many :package_item
def self.dump
Package.find(:all,
:select => "packages.*, packge_items.*")
end
end
class PackageItem < ActiveRecord::Base
belongs_to :package
belongs_to :product
end
I created Package.dump and it isn’t returning the package_items. What am I missing here?
You want this:
(Note: Passing a second option to
findis officially deprecated, and passing:all,:first, etc. tofindwill be deprecated in Rails 3.2 (I think). You should use the finder methodsPackage.all,Package.first, etc. instead. Basically the only time you should be usingfindis when you have an ID or IDs to pass as the only parameter(s).If you want you can create a scope for this, which is often preferable to explicitly defining a class method:
Then you can just call e.g.
Package.with_items.