0

If you want to setup a model with a belongsTo or hasMany relationship, you can do the following:

export default Model.extend({
    "client": DS.belongsTo('client',{
        async: true
    }),
    "days": attr(''),
    "cars": DS.hasMany('car',{ // somehow nest within days
        async: true
    }) 
});

However, what If I have a dynamic form? For example, I have a parking lot full of spaces. I create a new record to assign a client to my parking lot. I then start to assign spaces on different days of the week. Within each space, I want to know what car is parked where on a given day, allowed at a particular time.

Afaik, the JSON output would be something like this:

{
  "client" : "-EADn0NoAq65GRGOU-oQ",
  "days" : [ {
    "date" : "2016-11-11",
    "times" : [ {
      "when" : "morning",
      // want to somehow define a hasmany for which cars are being parked and in which lot number here
    } ]
  }, {
    "date" : "2016-11-12",
    "times" : [ {
      "when" : "afternoon"
      // want to somehow define a hasmany for which cars are being parked and in which lot number here
    } ]
  } ]
}

it looks like within a model, you only define the 'top level'. How do you define a relationship that goes beyond that top level? I want to be able to access a particular day, and then time... to see what cars are parked where, and with the hasMany relationship, I can then pull info from the car within another model... car type, model, milage, color etc.

Matt
  • 1,811
  • 1
  • 19
  • 30
  • 1
    Create more models ;) Probably you should have a model `day` and one `time` with on your root model `days: hasMany('day')` and then on the day `times: hasMany('time')`. However you consider if a single model that represents a timslot with both time&day could be a better solution. – Lux Nov 09 '16 at 00:46
  • @lux I was hoping there might be an 'easier' way, as this might create many unnecessary records that will not be associated with anything else or needed to be accessed on their own. – Matt Nov 09 '16 at 14:38
  • Well you *could* use no transform and output plain JSON. But I would recommend to use models. – Lux Nov 10 '16 at 00:26

1 Answers1

0

If I understand this correctly, I would have a time-slot model.

Each time-slot has a date and time, and belongsTo a client and car. You should be able to determine which cars are where, and for which client at any given time.

model/timeslot.js

export default Model.extend({
    "client": DS.belongsTo('client',{
        async: true
    }),
    "car": DS.belongsTo('car'),
    "bayNumber": DS.attr('number'),
    "day": DS.attr('date'),
    "time": DS.attr('string')
 })

Not entirely sure if this is what you were looking for, but hope it helps.

JonRed
  • 2,853
  • 5
  • 28
  • 37
  • Basically, I want to have a form with a button where I can add parking spots, within those spots per day, I can add additional times, and within those times... I can add details including an owner and/or a specific car from another model. The 'issue' is that creating another time-slot model, requires me to 'create those records' even though they're not finalized. In other words, if we're comparing a blog post to comments... I want to add a bunch a comments before they become actual 'records' that belong to blog posts. – Matt Nov 09 '16 at 20:08