Displaying has_and_belongs_to_many array in emberjs and rails - ruby-on-rails

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/

Related

Identification of duplicate multiples emails for user

I have User and UserEmails models.
User columns: id, first_name, last_name
UserEmails: id, user_id, email, primary
I need a function like this(https://imgur.com/a/762iK9r)
The essence is this: duplicate users are identified by or emails.
Example:
User 1:
{ id: 1, first_name: 'Test', last_name: 'User1' }
UserEmails for User 1:
{ email: 'same_email3#test.com', primary: true, user_id: 1 }
User 2:
{ id: 2, first_name: 'Test', last_name: 'User2' }
UserEmails for User 2:
{ { email: 'same_email#test.com', primary: true, user_id: 2 }, { email: 'same_email1#test.com', primary: false, user_id: 2 }, { email: 'same_email3#test.com', primary: false, user_id: 2 } }
User 3:
{ id: 3, first_name: 'Test', last_name: 'User3' }
UserEmails for User 3:
{ { email: 'same_email#test.com', primary: true, user_id: 3 }, { email: 'same_email3#test.com', primary: false, user_id: 3 }, { email: 'same_email4#test.com', primary: false, user_id: 3 } }
I need a query which will find that duplicates and return an ids array like this:
[[1, 2, 3], [...]]
I have a query but it incorrectly displays an array of ids:
ids = User.left_outer_joins(:user_emails)
.select('array_agg(users.id) AS ids')
.where('user_emails.email IS NOT NULL')
.group("user_emails.email")
.having('COUNT(1) > 1')
.map(&:ids)
I get this array, but the array [2, 3] is already in the array [1,2,3].
[[1, 2, 3], [2, 3]]
How I can group or sort the array ids that would not be duplicated in case of display of users which are duplicates by email?
You can try self join to find duplication
select distinct(u1.user_id) from public.UserEmails u1, public.UserEmails u2
where u1.email = u2.email

Passing API POST to stripe with business type?

Stripe news API update for Connect accounts doesn't allow the "legal_entity" param for new stripe accounts. The new updated way is for "business_type".. But the issue i have is that I need to pass data from either of the 2 choices for business_type of "individual" or "company.
This is the old way that worked:
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
legal_entity: {
first_name: stripe_account_params[:first_name].capitalize,
last_name: stripe_account_params[:last_name].capitalize,
type: stripe_account_params[:account_type],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
ssn_last_4: stripe_account_params[:ssn_last_4]
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
The new way (my attempt):
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
:business_type => stripe_account_params[:account_type],
requested_capabilities: ['card_payments'],
# company: {
# name: stripe_account_params[:business_name],
# phone: stripe_account_params[:business_phone],
# phone: stripe_account_params[:business_tax_id],
# address: {
# line1: stripe_account_params[:business_address_line1],
# city: stripe_account_params[:business_address_city],
# state: stripe_account_params[:business_address_state],
# postal_code: stripe_account_params[:business_address_postal]
# },
# },
individual: {
address: stripe_account_params[:address_line1],
first_name: stripe_account_params[:first_name],
last_name: stripe_account_params[:last_name],
ssn_last_4: stripe_account_params[:ssn_last_4],
# phone: stripe_account_params[:business_tax_id],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
With the section i have commented out, not commented out, I get this error:
(If i choose individual with the commented out area, it will work)
I tried simply not defining the address, etc. and loosely having the params and see if Stripe will decide where they go and that didn't work so it seems as if they need to be defined like above but i don't know how to distinguish them.
You cannot provide both company and individual parameters. Only
provide them accordingly with the business_type on the account.
Now, the fields are named the same within stripe:
https://stripe.com/docs/api/accounts/create
So i am unsure how i can pass this through. Any suggestions on how to do this?

Search in jsonb and array combination in postgres with rails4 stored_accessor

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

How to convert Json in Ruby on Rails?

I'm creating an API with Rails and Neo4j and i have a query looks like this on my model
Neo4j::Session.query.match('(User)').where("lower(User.first_name) =~ '.*#{params[:name].downcase}.*'").return('User')
this query return json looks like
[
{
User: {
user: {
username: null,
password: null,
first_name: "ayman",
last_name: "eldeeb",
email: "ayman#gmail.com",
phone: "44555",
avatar: "url",
birthdate: "1990-12-26"
}
}
}
]
Now, how to convert this json in Ruby on Rails to this below?
{
users: [
{
id: 0,
username: null,
first_name: "adham",
last_name: "eldeeb",
phone: "010220234",
email: null,
avatar: "url",
birthdate: null
}
]
}
Not sure, but this can probably work..
require 'json' #this has to be in the top
.....
#data = JSON.parse(Neo4j::Session.query.match('(User)').where("lower(User.first_name) =~ '.*#{params[:name].downcase}.*'").return('User'))
Now #data will be a ruby variable parsed from the JSON output..
Now to get your desired output, maybe the following snippet can help
#desired_data = []
#data.each do |d|
#desired_data << d[:User][:user] # this could be d["User"]["user"] if this dosen't work
end

How to make root json on rails?

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

Resources