1

I know how to include nested resources in controller render like this:

render :json => @user, :include => {:profile}

My doubt is: is it possible to do this in models?

I tried many ways but it doesn't give me the user and his profile like this:

{
    id: 1,
    name: 'John',
    profile: {
        id: 64,
        status: 'active'
    }
}

I tried this in my model:

User.where(id: 1).includes(:profile)

And the output was:

User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
Profile Load (0.2ms)  SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` IN (1)
Seralto
  • 1,016
  • 1
  • 16
  • 30
  • What do you mean by "in models"? Show how you want to access it and how it isn't working. You can do an include :profile when you fetch the model like: User.where(name: 'John').includes(:profile).all This will make two queries, one for all users named "John" and another for all of their profile records. – Sixty4Bit Jun 01 '15 at 19:00
  • Hi Sixty4Bit, I am trying to use "includes", but it is not working (even with your example). It only gives me the user. – Seralto Jun 01 '15 at 19:16

1 Answers1

1

Edit: Ignoring render. If you want to include associated models in a JSON output just call [to_json with include as an option][1].

Edit two: Based up our chat it looks like you're wanting to get to objects, not JSON data.

Using your example user record:

users = User.includes(:profile).where("YOUR CONDITIONS") 
users.each do |user|
  puts user.profile.status #or do something with your loop
end
creativereason
  • 1,524
  • 1
  • 11
  • 20
  • 1
    Also, [`jbuilder`](https://github.com/rails/jbuilder) is a great way to build very specific JSON options at the controller level. – creativereason Jun 01 '15 at 19:21
  • My render is working perfectly, the problem is when I tried to access inside my model like this: User.includes(:profile). – Seralto Jun 01 '15 at 19:21
  • Are you saying the includes doesn't work because you aren't getting data for `profile`, or is your JSON is returning in the incorrect format? – creativereason Jun 01 '15 at 19:23
  • calling your render like I show in my answer should include both `user` and `profile` in a `JSON` response. `to_json` allows you include nested models. – creativereason Jun 01 '15 at 19:32
  • I don't want to render, I need this data in my model to do other things. I am returning it just to debug the data. – Seralto Jun 01 '15 at 19:33
  • I added more details to the answer to show what I mean. – creativereason Jun 01 '15 at 19:46
  • OK. Now suppose I need to get the user status that is 'active' in this case, to perform something. How can I do this? – Seralto Jun 01 '15 at 20:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79358/discussion-between-creativereason-and-sergio-toledo). – creativereason Jun 01 '15 at 20:06
  • 1
    Correct creative, I can get all users, iterate over them, and get what I want. Thanks. – Seralto Jun 01 '15 at 20:41