Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4838

How do I configure a many-to-many relationship?

$
0
0

@Dahnaya_Joyner wrote:

I have a many to many relationship between users and accounts. When I create these records on sign up, I am able to see the id of the account I just created in the json for the users I just created, but I am not able to see the user ids in the account json I just created. I am trying to create these records with the associated ids. I am using Sinatra for the back end. This is my first time asking a question so my apologies if my question isn’t clear or code snippets aren’t formatted correctly. Here’s what I have:

user model

export default DS.Model.extend({

...

accounts: DS.hasMany('account'),

...
});

account model

export default DS.Model.extend({
...

users: DS.hasMany('user'),

...

})

sign-up route

model () {
    console.log("at model() on route for /sign-up");

    let account = this.store.createRecord('account');
    let dentist = this.store.createRecord('user');
    let officeContact = this.store.createRecord('user');


    let item = RSVP.hash({
        dentist: dentist,
        officeContact: officeContact,
        account: account,
    });

    return item;

},

actions: {
    register (item) {
        console.log('at register action on route for /sign-up');


        item.account.save()
            .then(function() {
                item.account.get('users').addObject(item.dentist);
                item.account.get('users').addObject(item.officeContact);

                item.dentist.save().then(function() {
                    item.officeContact.save()
                })
            }).then(() => this.transitionTo('/'))


        console.log("//ACCOUNT//");
        console.log(item.account);
        console.log("//DENTIST//");
        console.log(item.dentist);
        console.log("//OFFICE CONTACT//");
        console.log(item.officeContact)
    }
}
})

POST users

post '/v1/users' do
request.body.rewind
json = JSON.parse(request.body.read)
puts "#{json}"
account = Account.first(id: json['user']['accounts'][0].to_i)
json['user'].delete('accounts')
json['user'].delete('password_confirmation')
puts "USER #{json['user']}"
puts "USER ACCOUNT #{json['user']['type']}"

user = User.create(json['user'])
user.accounts << account
if user.save == false
  user.errors.each do |err|
    puts "NEW USER ERROR #{err}"
  end
  halt 400, '{"message": "User not created."}'
end

{user: user}.to_json
end

POST Account

request.body.rewind
json = JSON.parse(request.body.read)
puts "ACCOUNT #{json['account']}"
account = Account.create(json['account'])
if account.save == false
  account.errors.each do |err|
    puts "ACCOUNT #{json['account']}"
    puts "NEW ACCOUNT ERROR #{err}"
  end
  halt 400, '{"message":"Could not create Account"}'
end
puts "ACCOUNT #{json['account']}"

{account: account}.to_json
 end

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4838

Trending Articles