ruby on rails - ActiveModel Serializer sending all records instead of specified limited amount -


i have controller looks this:

class metricscontroller < applicationcontroller   def index     org = current_user.organization     metrics = metric.where(organization: org).last(100)     render json: metrics, status: 200, include: [:organization]   end end 

metrics model:

class metric < activerecord::base   belongs_to :organization end 

organization:

class organization < activerecord::base   has_many :metrics end 

metricserializer:

class metricserializer < activemodel::serializer   embed :ids   attributes :id, :provisioned_users, :connected_users, :created_at    has_one :organization  end 

organizationserializer:

class organizationserializer < activemodel::serializer   attributes :id, :name, :org_id, :gauth_enabled    has_many :metrics end 

the json though gets returned

organizations: [   {     id: 1,     name: "acme",     org_id: "org_id232323",     gauth_enabled: "f",     metric_ids: [       1,       2,       3,       ...       1000     ]   } ], 

as can see serializer spitting out every record in metric's table when presumably want last 100. i'm unsure i've set wrong in ams (0.8).

interestingly rails console shows right sql correct limits:

metric load (0.7ms)  select  "metrics".* "metrics"   order "metrics"."id" desc limit 100   organization load (0.3ms)  select  "organizations".* "organizations"  "organizations"."id" = $1 limit 1  [["id", 1]] 

check out guides on associations:

by default, serializers association on original object. can customize behavior implementing method name of association , returning different array.

so, each model gets serialized according serializer. organization models being put through organizationserializer, had has_many :metrics specified. thus, rails looked of metrics organization, , serialized them.

you didn't want that, cared 1 organization metric belonged to. remove line, , rails won't serialize rest.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -