ruby - Associations made in Rails API not serializing compatibly with Ember app -


i created rails api (using rails-api gem) consumed ember app (using ember-cli).

my problem associations have set in api don't seem yield ember app looking for, , don't have idea needs changed or done differently.

below, describes , steps took here.

  1. i generated 2 scaffolds:

    rails g scaffold movement name:string movement_type:string  rails g scaffold record max_effort:integer max_type:string movement:references 

    which produced:

    models

    class movement < activerecord::base end  class record < activerecord::base   belongs_to :movement end 

    serializers

    class movementserializer < activemodel::serializer   attributes :id, :name, :movement_type end  class recordserializer < activemodel::serializer   attributes :id, :max_effort, :max_type   has_one :movement end 

    migrations

    class createmovements < activerecord::migration   def change     create_table :movements |t|       t.string :name       t.string :movement_type        t.timestamps null: false     end   end end  class createrecords < activerecord::migration   def change     create_table :records |t|       t.integer :max_effort       t.string :max_type       t.references :movement, index: true, foreign_key: true        t.timestamps null: false     end   end end 
  2. i ran bundle exec rake db:migrate

    which produced:

    schema

    activerecord::schema.define(version: 20150514184239)    create_table "movements", force: :cascade |t|     t.string   "name"     t.string   "movement_type"     t.datetime "created_at",    null: false     t.datetime "updated_at",    null: false   end    create_table "records", force: :cascade |t|     t.integer  "max_effort"     t.string   "max_type"     t.integer  "movement_id"     t.datetime "created_at",  null: false     t.datetime "updated_at",  null: false   end    add_index "records", ["movement_id"], name: "index_records_on_movement_id"  end 
  3. i created seed data (using faker gem) , run bundle exec rake db:seed

    seeds (rails/app/db/seeds.rb)

    movements = array.new  10.times |movement|   movement = movement.create(     name: faker::lorem.word(),     movement_type: ['pushing', 'pulling', 'squatting'].sample()   )   movements.push(movement) end  30.times   record.create(     movement: movements.sample(),     max_effort: faker::number.between(1, 100),     max_type: ['rep', 'sec'].sample()   ) end 

    when run rails s, respective urls show:

    at http://localhost:3000/movements

    {   movements: [     {       id: 1,       name: "dolor",       movement_type: "pushing"     },     {       id: 2,       name: "suscipit",       movement_type: "squatting"     }     ...   ] } 

    at http://localhost:3000/records

    {   records: [   {     id: 1,     max_effort: 90,     max_type: "rep",     movement: {       id: 7,       name: "quia",       movement_type: "pushing"     }   },   {     id: 2,     max_effort: 26,     max_type: "sec",     movement: {       id: 3,       name: "non",       movement_type: "pushing"     }   }   ... } 
  4. i set ember adapter, serializer, , router ams compatibility

    adapter

    export default ds.activemodeladapter.extend({ }); 

    serializer

    export default ds.activemodelserializer.extend({ }); 

    router

    var router = ember.router.extend({   location: 'auto' });  export default router.map(function() {   this.resource('movements', function() {     this.resource('movement', {path: ':id'}, function() {       this.route('records');     });   }); }); 
  5. i set same associations in rails ember models

    movement

    export default ds.model.extend({   name:         ds.attr('string'),   movementtype: ds.attr('string'),   records:      ds.hasmany('record') }); 

    record

    export default ds.model.extend({   maxeffort: ds.attr('number'),   maxtype: ds.attr('string'),   movement: ds.belongsto('movement') }); 
  6. i set respective ember routes

    movement (app/routes/movement.js)

    export default ember.route.extend({   model: function(params) {     return this.store.find('movement', params.id);   } }); 

    records (app/routes/movement/records.js)

    export default ember.route.extend({   model: function() {     return this.modelfor('movement').get('records');   } }); 
  7. i run ember s --proxy http://localhost:3000

    results:

    all movements populated , behaving expected in app's ui. however, not case associated records. ember inspector reveals no records have been loaded.

concluding thoughts:

it seems me movements json returned rails should show reference records. attempted adding explicit association both movement model , serializer:

    class movement < activerecord::base       has_many :records       end      class movementserializer < activemodel::serializer       attributes :id, :name, :movement_type       has_many :records     end 

this created error when tried run rails server.

my other hunch seeds.rb code problem. first time using faker, , i'm still no expert in ruby.

i stuck, , sure lack of experience 1 of, or both, ruby/rails , ember. hope have included here easy mistake more experienced spot.

thank you.

your records json, should this, according ember json convention http://guides.emberjs.com/v1.10.0/models/connecting-to-an-http-server/

{   records: [   {     id: 1,     max_effort: 90,     max_type: "rep",     movement_id: 3   }],   movements: [   {     id: 7,     name: "quia",     movement_type: "pushing"   }   {     id: 3,     name: "quia",     movement_type: "pushing"    }  ], } 

deep nesting kind of hard, if absolutely necessary, in using in same template own outlets. if not make separate route.

also if struggling deep nesting can create own custom rest adapter pointing url.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -