I'm sorry I'm not good in English.
I am trying to find topic as same my problem.My problem is json not root.
This is my json data:
[
{
created_at: "2013-07-26T02:49:55Z",
description: "hgfhgfhf",
iconapp: null,
id: 13,
name: "test1",
updated_at: "2013-07-26T02:49:55Z",
},
{
created_at: "2013-07-26T02:54:40Z",
description: "sadsadas",
iconapp: null,
id: 14,
name: "asdadas",
updated_at: "2013-07-26T02:54:40Z",
}
]
but I want to my json data have root element like this:
- Apps: [
{
created_at: "2013-07-26T02:49:55Z",
description: "hgfhgfhf",
iconapp: null,
id: 13,
name: "test1",
updated_at: "2013-07-26T02:49:55Z",
},
{
created_at: "2013-07-26T02:54:40Z",
description: "sadsadas",
iconapp: null,
id: 14,
name: "asdadas",
updated_at: "2013-07-26T02:54:40Z",
}
]
Json root is "Apps".How do I do ?
Help me please ?
Its a configuration thing. By default the root is not included in json. You can enable it via:
ActiveRecord::Base.include_root_in_json = true
If you want it application wide add it to a new initializer that will run the code when the app is started. Its documented here: http://apidock.com/rails/ActiveRecord/Serialization/to_json
Related
When I create a new admin user, there are three objects of translations:
Admin.last.translations.map(&:locale) # => [:ru, :uz, :uz]
Why are the locales duplicated?
Example:
admin.position_uz = 'CTO'
admin.position_ru = 'CTO'
admin.save!
admin.translations.count = 3
admin.translations.first.position = 'CTO'
admin.translations.second.position = 'Another value or empty'
admin.translations.third.position = 'CTO'
Every time the locale is set to uz, it shows the second variant.
Admin translations loo like
[
#<Admin::Translation id: 1, admin_id: 2, locale: "ru", position: "CTO", created_at: "2019-01-16 06:24:17", updated_at: "2019-01-16 06:30:31">,
#<Admin::Translation id: 2, admin_id: 2, locale: "uz", position: "Board Member", created_at: "2015-07-26 20:42:18", updated_at: "2015-07-26 20:42:18">,
#<Admin::Translation id: 3, admin_id: 2, locale: "uz", position: "CS manager", created_at: "2019-01-16 06:24:17", updated_at: "2019-01-16 07:09:21">
]
I have a model called Event, where I have stored_accessor "list" (stored like data: {"list"=>[{"key"=>"key1", "value"=>"value1"}]}).
I need to make a search query o
#<Event id: "1", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key1", "value"=>"value1"}, {"key"=>"key2", "value"=>"value2"}]}, created_at: "2017-04-14 21:06:22", updated_at: "2017-04-20 10:36:08">
#<Event id: "2", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key1", "value"=>"value1"}]}, created_at: "2017-04-14 21:06:22", updated_at: "2017-04-20 10:36:08">
#<Event id: "3", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key11", "value"=>"value11"}, {"key"=>"key12", "value"=>"value12"}]}, created_at: "2017-04-14 21:07:22", updated_at: "2017-04-20 10:37:08">
#<Event id: "4", title: "HHHH", description: nil, data: {"list"=>[{"key"=>"key111", "value"=>"value111"}, {"key"=>"key112", "value"=>"value112"}]}, created_at: "2017-04-14 21:08:22", updated_at: "2017-04-20 10:38:08">
I have a serach params like
1) {'key'=> 'key1', 'value'=> 'value1'}
2) ["key"=>"key1", "value"=>"value1"}, {"key"=>"key2", "value"=>"value2"}]
In first case, it should return Event id 1 and 2.
In second case, it should return Event id 1. (event if return 1 and 2 both could be acceptable).
I am not sure with json and array combination.
Please help.
You may do it with PostgreSQL jsonb's operator #>. Also you need to write the full path for search params: {'list' => [{'key'=> 'key1', 'value'=> 'value1'}]}. Try this code:
to_contain1 = {'list' => [{'key'=> 'key1', 'value'=> 'value1'}]}
to_contain2 = {'list' => [{'key'=> 'key2', 'value'=> 'value2'}]}
Event.
where("data #> ?", to_contain1.to_json})
# returns events 1 & 2
Event.
where("data #> ?", to_contain1.to_json).
where("data #> ?", to_contain2.to_json)
# returns event 1
When I call:
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
I get this active record realtion:
[#<CodeLesson id: 2, name: "Python", permalink: "python", lesson_content: "", instructions: "Print your name to the console.", hints: "", starting_code: "\"\"\"\r\nThis is a comment\r\n\"\"\"\r\n\r\nprint(\"Hello, World\"...", language_id: "12", order: 1, track_id: 2, user_id: 1, created_at: "2014-02-14 16:01:12", updated_at: "2014-02-15 21:14:43", visible: true>, #<CodeLesson id: 8, name: "Test Lesson", permalink: "test-lesson", lesson_content: nil, instructions: nil, hints: nil, starting_code: nil, language_id: "26", order: nil, track_id: 2, user_id: 1, created_at: "2014-02-20 19:23:15", updated_at: "2014-02-20 19:23:15", visible: false>]
How do I convert this into a usable array of models so I can do something like this:
preivous_lessons.each do |i|
highest = i.order if i.order > highest
end
As OP confirmed from my comment, that my hint solved his problem, I am putting it as an answer to the post :
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
highest = preivous_lessons.maximum(:order)
Documentation of maximum :
Calculates the maximum value on a given column. The value is returned with the same data type of the column, or nil if there's no row.
preivous_lessons = #item.where("track_id = ?", lesson.track_id).all
I've got below JSON returning from an API endpoint
{
users: [
{
id: 3,
email: "example#gmail.com",
title: "Mr",
first_name: "Hi",
last_name: "Hey",
position: "Web Dev",
work_phone: "123456",
company: "Comp",
sign_in_count: 0,
last_sign_in_ip: null,
confirmed_at: null,
created_at: "2013-11-08T03:30:21.160Z",
roles: [
{
id: 2,
name: "booth_rep",
resource_id: null,
resource_type: null,
created_at: "2013-11-11T06:14:16.062Z",
updated_at: "2013-11-11T06:14:16.062Z"
}
]
}]
}
Is there a way to use this with Emberjs and Emberdata to display role name in my users handlebars template?
Does it have to be specified in the model?
It looks like they added many to many in this commit:
https://github.com/emberjs/data/commit/7f752ad15eb9b9454e3da3f4e0b8c487cdc70ff0
So so all you need to do is define the model
App.User = DS.Model.extend({
...
roles: DS.hasMany();
});
App.Role = DS.Model.extend({
...
users: DS.hasMany();
});
Then in your user template
{{#each role in roles}}
{{role.name}}
{{/each}}
I'm not sure that I understand your question correctly, but you can return a result from $.getJSON from your routes model method, if you want:
App.IndexRoute = Ember.Route.extend({
model: function() {
return $.getJSON( ... );
}
});
I created a simple example, which displays a couple of info from your data with handlebars:
http://jsfiddle.net/x3CEU/
On rails console output what does it mean #< at the start of the hash on the following example?:
irb(main):003:0> a=Movie.all
Movie Load (0.5ms) SELECT "movies".* FROM "movies"
=> [#<Movie id: 1, title: "Aladdin", rating: "G", description: nil, release_date: "1992-11-25 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, # <Movie id: 2, title: "The Terminator", rating: "R", description: nil, release_date: "1984-10- 26 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, #<Movie id: 3, title: "When Harry Met Sally", rating: "R", description: nil, release_date: "1989-07-21 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">,... more output
That's how an object is printed in ruby, for example and instance of class Movie would be printed something like this:
<#Movie:0x003247fa... >
| |
class memory position I think
What you have there is a set of this previous writing:
[ one_object, other_object ... ]
To have a better display you could use hirb.
#< means that this is an instance of the Movie class.