0

I want to be able to make an ActiveRecord call (or anything else that might get me the right results) that will pass me all object attributes. For example:

I have a contact model, which can have many "CustomField"'s and many "ServiceAddress"'s

contact =
      id:"5"
      custom_fields:[{
        id:"5"
        name:"birthday"
        label:"birthday"
      }]
      service_addresses: [{
        id: 'test1'
        type: 'email'
        address: 'a@a.com'
        kind: 'home'
      },{
        id: 'test2'
        type: 'email'
        address: 'b@a.com'
        kind: 'other'
      },{
        id: 'test3'
        type: 'email'
        address: 'd@a.com'
        kind: 'other'
  }]
  first_name:"emily"
  kind:”person"

Is there some kind of query I can make to receive something similar?

Thanks!

Mark Silverberg
  • 1,249
  • 2
  • 8
  • 21
emag3m
  • 75
  • 5
  • I think you are looking for something called "eager loading" or "preloading" (this is as opposed to lazy loading). Take a look at http://blog.arkency.com/2013/12/rails4-preloading/ which has some good information but long story short, I think you want to be doing `Contact.includes(:services_address, :custom_fields).where(...)` – Mark Silverberg Aug 15 '14 at 19:44

1 Answers1

1

You can use includes to specify the associations to load:

Contact.includes(:custom_fields, :addresses).find(5)
infused
  • 24,000
  • 13
  • 68
  • 78