1

I'm using rails only as a backend to serve JSON data to the Ember frontend application.

My DB Structure is quite concatenated, what leeds to rails loading the complete database on every request.

How can i deal with that?

For example, I got a blog, blog "has_many" articles, articles "has_many" comments and so on. When I want to view a blog I don't care about the comments, all I need are the articles list.

So how can I prevent rails from resolving the "has_many" relationship on articles, when I don't need them?

EugZol
  • 6,476
  • 22
  • 41
2mb13r
  • 11
  • 3
  • 3
    If you use `Blog.find(ID)` Rails will not autoload all the associated records. If it does, then you may have an error somewhere. Please provide more data to use for debugging as code examples, query logs, etc. – Simone Carletti Aug 17 '15 at 07:40
  • Alright, i'll provide some code later today, when i get back to my pc. but technically: everithing is quite simple:i have a serializer defining attributes, relationships and embed `embed :ids, include: true attributes :id, :name, :content has_many :comments` and controller responding with JSON `respond_to :json def index respond_with Article.all end` – 2mb13r Aug 17 '15 at 10:10
  • Duplicate of http://stackoverflow.com/questions/16311989/limiting-associations-cascade-in-active-model-serializer – EugZol Aug 17 '15 at 13:49
  • because of the Deprication warning a added `ActiveModel::Serializer.setup do |config| config.embed = :ids config.embed_in_root = true end` to my config file .... that meight be a problem ... – 2mb13r Aug 19 '15 at 18:46

1 Answers1

0

i think i've found the sollution ... not sure if it's the best way, but it works ... i add some conditions to the controller, now it looks like:

def index
    @offices = Office.all
    render json: @offices.as_json(
        only: [:id, :title, :created_at],
        include: { 
            addresses: { 
                except: [:contacts] 
            } 
        }
    )
end
2mb13r
  • 11
  • 3