How to get user object with belongs_to in ember js? - ruby-on-rails

I have two models Recipe and User. I can't get the user object in recipe. What am I doing wrong?
App.Recipe.find(1).get('title') // Returns "recipe01 title". All works fine.
App.Recipe.find(1).get('user') // Returns null
user.js.coffee
App.User = DS.Model.extend
email: DS.attr('string')
recipes: DS.hasMany('App.Recipe')
recipe.js.coffee
App.Recipe = DS.Model.extend
user: DS.belongsTo('App.User')
title: DS.attr('string')
my json array
{
recipe: {
id: 1,
title: "recipe01",
user: {
id: 1,
name: "ejiqpep",
email: "ejiqpep#gmail.com",
}
}
}

By default Ember Data expects dependent records to be referenced by key. Then you can either sideload the extra records or let Ember lazy load them from their API endpoint.
{
recipe: {
id: 1,
title: "recipe01",
user_id: 1
},
user: {
id: 1,
name: "ejiqpep",
email: "ejiqpep#gmail.com"
}
}
However, you can also instruct the Adapter that the records are embedded.
There are two types of embedded record loading embedded: 'always', where Ember will both receive and send any changes with the objects embedded.
App.Adapter.map 'App.Recipe',
user:
embedded: 'always'
Or embedded:'load' where Ember will load embedded objects from JSON but will save changes back to the API as separate objects.
App.Adapter.map 'App.Recipe',
user:
embedded: 'load'
Which of the three options you'd like to take is up to you.
Embedded objects have only recently been implemented and there are a couple of issues around them (see the Ember-Data issues on Github), but will work without any changes to your existing server.

Related

Automatically remove associated records if not present in update params

We're using Rails 5.2.2.1 as an API with Vue for the frontend.
A Post has_many :tags. A Post also accepts_nested_attributes_for :tags, allow_destroy: true.
When creating a post, you create the tags at the same time and this works well. When editing tags, however, I'm running into an issue where tags that aren't present in the update payload are not deleted from the database. Here's an example of what I'd expect:
# A Post has currently tags with ID 1, 2, and 3.
# This (pseudo) update payload is sent
{
tags_attributes: [
{
id: 1,
name: 'Tech'
},
{
id: 3,
name: 'Sports'
},
{
id: nil,
name: 'Science'
}
]
}
# The post's tag with an ID of 2 is not present in the update payload, so it should be automatically removed
# Since we're also passing up a new tag without an ID, this tag should be created
I have a solution involving _destroy: '1', but it would be preferable for tags to be automatically marked for destruction based on their presence in the update payload.

Subnodes in a json template

I have a template for json using the JB gem: index.json.jb
https://github.com/amatsuda/jb
It outputs all the settings with some attributes per record.
Settings also have a metadata field, which is a postgres jsonB field. I'd like to loop through the metadata fields contents and add it to my json template. I've tried all sorts of ways and I can't figure out how to do this.
json = {}
json[:settings] = #settings.map do |setting|
{
id: setting.id,
category: setting.category,
created_at: setting.created_at,
updated_at: setting.updated_at,
notes: setting.notes,
name: setting.full_name,
setting.metadata.map do |meta|
{
meta[0]: meta[1]
}
end
}
end

How to save metadata into database from a rake file (rails)?

I am building a script to scrape data from a website. You can see the full code here: Undefined method 'click' for nil:NilClass (Mechanize)
Anyways, I have trouble to save this metadata into the database:
members = member_links.map do |link|
member = link.click
name = member.search('title').text.split('|')[0]
institution = member.search('td~ td+ td').text.split(':')[0]
dob = member.search('.birthdate').text.strip[1..4]
{
name: name.strip,
institution: institution.strip,
dob: dob,
bio: bio
}
end
Thus, how can I accomplish this?
Well, it should be as simple as:
Datum.create!(
name: name.strip,
institution: institution.strip,
dob: dob,
bio: bio
)
This is very basic Rails knowledge, make sure you've read Active Record Basics guide.

Mongoid does not save the _type of a collection

I have here the behaviour that in one of my collection the _type field was not populated, what in my case is really needed.
scenario 'User edits a session' do
session = survey.sessions.create version: version, token: 'xxx'
produces a database entry like that without , _type: 'Helena::Session'...
{ _id: ObjectId("5458f7b16861632f09020000"),
token: "xxx",
version_id: ObjectId("5458f7b16861632f09010000"),
completed: false, survey_id: ObjectId("5458f7b16861632f09000000"),
updated_at: ISODate("2014-11-04T15:58:41.541Z"),
created_at: ISODate("2014-11-04T15:58:41.541Z"),
view_token: "VEXfJNjyr3yI4GIziqTxrsHC9" }
I explicit have to pass the type. Is there a way to enforce the _type even if it's not a derived object?

Modify data structure of CarrierWave image uploader

I'm using the ruby gem CarrierWave to handle images on my rails app, which is a mobile backend API. Is there a way to store the image url and thumb url directly on the parent object?
This is the default behavior, shown in the post object's json. Notice the nested JSON:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture: {
url: "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb: {
url: "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
}
},
updated_at: "2012-11-26T08:16:43Z"
}
What I'd like to see:
{
created_at: "2012-11-17T18:24:04Z",
description: "this is the content",
id: 6,
user_id: 1,
picture_url = "/uploads/entry/picture/6/wtf_llama.jpeg",
thumb_url = "/uploads/entry/picture/6/thumb_wtf_llama.jpeg"
updated_at: "2012-11-26T08:16:43Z"
}
Is this possible?
Why storing those paths in models? Improve response (view or controller), not persistence layer (model). I believe it's easiest to achieve with as_json. Either add picture_url methods to model or merge additional entries to your final hash.

Resources