Identification of duplicate multiples emails for user - ruby-on-rails

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

Related

Typeorm get nested relations without intermediate table

I have Many-to-many relations with custom properties like here
I try to get a post with all category like
PostRepository.findOne({
relations: ['postToCategories.category'],
where: { id: 1},
})
the results like this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, category: {id: 1, name: "cate1"}}]
}
But i don't want id of intermediate table. I need something like this
{
id: 1,
name: 'post1',
postToCategories: [{category: {id: 1, name: "cate1"}}]
}
or this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, name: "cate1"}]
}
I don't need id of postToCategories. How to do that thank.

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

Fetching wrong data in query in rails

I have done this
def show_selected_students(selected_students, students)
student = students.map{|a| a.name}
selected_students = selected_students.split(",")
#student_selected = selected_students.map {|i| student[i.to_i] }
end
in students I am fetching given data
#<ActiveRecord::Relation [#<Student id: 1, name: "XYZ",>, #<Student id: 2, name: "test1">, #<Student id: 3, name: "cherry">, #<Student id: 4, name: "mary">, #<Student id: 5, name: "hary">, #<Student id: 35, name: "hen">, #<Student id: 44, name: "duck">, #<Student id: 62, name: "try">]>
and in selected_students I am getting 2,3,4 Now I want to fetch those students whose id match with selected_students for this I had written this but it gives me this output ['cherry', 'mary', 'hary'] i.e id 3,4,5 but I want 2,3,4 Please guide me how to solve this. Thanx in advance.
You can try this:
def show_selected_students(selected_students, students)
selected_students = selected_students.split(",")
#student_selected = students.where(:id => selected_students).map{|a|a.name}
end

Displaying has_and_belongs_to_many array in emberjs and 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/

Resources