1

This could be a very basic question but forgive my limitless of my knowledge. How would you represent Js nested object (Backbone Model) in Rails Model ?

var UserSchema = new mongoose.Schema({
  _id: ShortId,
  name :{
    first: {type: String},
    last: {type: String}
  }
});

1 Answer 1

1

The Mongoid model would be

class User
  include Mongoid::Document
  field :first, type: String
  field :last, type: String
end

There's the issue of mapping from Mongoid/MongoDB "_id" to Backbone "id". The choices to address this are summarized in several discussions that can be Googled, an example is http://dzello.com/blog/2011/12/24/tame-the-mongoid-id-field-in-your-rails-and-backbone-js-app/

Different people have chosen either of the two solutions, telling Backbone to use "_id" or rewriting #to_json in Mongoid to "id". Either will work, you may want to experiment to see what you prefer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.