In plain English, I am trying to find all agents who have a license in the state of Ohio and have an industry name of Professional Beer...
Here are my models:
class Agent < ApplicationRecord
validates :name, :phone_number, presence: true
has_many :licenses
has_and_belongs_to_many :carriers
end
class Carrier < ApplicationRecord
validates :name, presence: true
has_and_belongs_to_many :industries
end
class Industry < ApplicationRecord
validates :name, presence: true
has_and_belongs_to_many :carriers
end
class License < ApplicationRecord
validates :state, presence: true
belongs_to :agent
end
I have worked out both ends of the queries:
2.5.3 :057 > Carrier.joins(:industries).where('industries.name = "Professional Beer Taste-Tester"')
Carrier Load (0.5ms) SELECT "carriers".* FROM "carriers" INNER JOIN "carriers_industries" ON "carriers_industries"."carrier_id" = "carriers"."id" INNER JOIN "industries" ON "industries"."id" = "carriers_industries"."industry_id" WHERE (industries.name = "Professional Beer Taste-Tester") LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Carrier id: 6, name: "Iguana Insure", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Carrier id: 10, name: "Polaris International Risk", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">]>
and...
2.5.3 :061 > Agent.joins(:licenses).where('licenses.state = "OH"')
Agent Load (0.4ms) SELECT "agents".* FROM "agents" INNER JOIN "licenses" ON "licenses"."agent_id" = "agents"."id" WHERE (licenses.state = "OH") LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Agent id: 5, name: "Scot Morar", phone_number: "(279) 691-4148", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 6, name: "Cassey Beier", phone_number: "(474) 777-3615", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 12, name: "Jani VonRueden", phone_number: "1-444-626-9429", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 14, name: "Gemma Ritchie", phone_number: "594.170.4795", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 20, name: "Milo Klein", phone_number: "677.137.7019", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 40, name: "Ellie Cole", phone_number: "(720) 543-2929", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 52, name: "Dayna Hilll", phone_number: "(220) 218-4786", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 54, name: "Barton Nikolaus II", phone_number: "1-881-355-1168", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 61, name: "Miss Katrice Oberbrunner", phone_number: "1-658-789-4817", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 66, name: "Hobert Ryan DDS", phone_number: "501-950-6320", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, ...]>
=> #<ActiveRecord::Relation [#<Carrier id: 6, name: "Iguana Insure", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Carrier id: 10, name: "Polaris International Risk", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">]>
Now I need to combine both of these so I am returning a list of agents. So the desired result would return a list of agents:
=> [#<Agent id: 118, name: "Deangelo Hermiston", phone_number: "1-756-870-1969", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 119, name: "Wm Breitenberg", phone_number: "223-404-1670", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">, #<Agent id: 120, name: "Elsy Vandervort", phone_number: "908.065.9697", created_at: "2019-08-21 15:38:30", updated_at: "2019-08-21 15:38:30">]
For a single query there isn't adequate relations setup. So what can be done is
ohio_agents_id = Agent.joins(:licenses).where('licenses.state = "OH"').pluck(:id)
desired_agents = Agent.joins(carriers: :industries).where(industries: { name: "Professional Beer Taste-Tester" }, id: ohio_agents_id)
Below will get desired result in single query
Agent.joins(:licenses, carriers: :industries).where('licenses.state = "OH"').where('industries.name = "Professional Beer Taste-Tester"')
Related
Please help
I have table ideas that look like this
ideas = {
id,
title,
abstract,
background,
classification,
technicalfeild,
tag,
user_id,
}
and a table idea-images that look like this
idea-images = {
id,
idea_id,
banner,
image_details
}
they have a one to many relations. I need to select all the ideas that have public tags and find the images in the idea-image table and attached the images as an array to the idea since the idea can have many images.
here is my code
return knex('ideas')
.where('tag', 'published')
.rightJoin('idea-images', 'ideas.id', '=', 'idea-images.idea_id')
.orderBy('ideas.created_at', 'desc');
and here what it will return
[
{
id: 1,
title: 'blahhh ',
abstract: ' blahh',
background: 'blahhh',
classification: 'consumer product',
technicalfeild: '',
description: 'blah....... ',
summary: '',
claims: 'blahhhh',
tag: 'published',
user_id: 3,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 2,
banner: 'Drawing-2',
image_details: 'blahhh',
},
{
id: 3,
title: 'blahhh ',
abstract: ' test',
background: 'test',
classification: 'blahhhh',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test',
image_details: 'test',
},
{
id: 4,
title: 'My new car ',
abstract: ' test',
background: 'test',
classification: 'consumer product',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test2',
image_details: 'test2',
}
]
for some reason it creates a new idea if the idea has 2 images!!! I really have no idea how to make the image data into an array.
There are 2 possible solutions,
Using group_concat, suitable when each idea has small amount of images, the query looks like
https://www.db-fiddle.com/f/74qcRt3siGF2sxT4ykqh9t/1
Split into 2 queries, fetch ideas and then fetch all images by relevant ideas
const ideas = await knex('ideas')
.where('tag', 'published')
.orderBy('ideas.created_at', 'desc');
const ideaImages = await knex('ideaImages').whereIn('ideaId', ideas.map(idea => idea.id));
mergeData(ideas, ideaImages);
Something strange is happening in my views, in a rails 5 app. Everytime I display a view I get output from My console displaying in my index views
I have searched everything online and cannot find a solution to stop it from displaying...
index action in my controller:
def index
if params[:category].blank?
#books = Book.all.order("created_at DESC")
else
#category_id = Category.find_by(name: params[:category]).id
#books = Book.where(:category_id => #category_id).order("created_at DESC")
end
end
Whats appearing in my view
Jaws
Michael Collins
Messi
The Assination of Jesse James
Pocket Reference JavaScript
A Game of Thrones: The Illustrated Edition: A Song of Ice and Fire: Book 3
The Lion The Witch and The Wardrobe
The Rise of The Islamic State
1984
The Principles of Objected Oriented JavaScript
Dracula
No Country for Old Men
Game of Thrones: A Storm of Swords; Steel and Snow, Book 3, Part 1
[#<Book id: 19, title: "Jaws", description: "kdmfvkmvlsf", author: "Stephen King", created_at: "2018-07-22 22:14:54", updated_at: "2018-07-22 22:14:54", user_id: 1, category_id: 5>, #<Book id: 18, title: "Michael Collins ", description: " jnkefjn", author: "Tim Pat Coogan", created_at: "2018-07-22 22:09:34", updated_at: "2018-07-22 22:09:34", user_id: 1, category_id: 3>, #<Book id: 17, title: "Messi", description: "SDNFKJVVSFNVJKNVFJ", author: "Guillem Balague ", created_at: "2018-07-22 20:54:38", updated_at: "2018-07-22 20:54:38", user_id: 2, category_id: 3>, #<Book id: 16, title: "The Assination of Jesse James ", description: "jsdnfvjksnvjnfvfsnjvsn jdfnvjnsfvkjnvkjnfdj jefnvj...", author: "Jesse James", created_at: "2018-07-22 20:37:03", updated_at: "2018-07-22 20:37:03", user_id: 2, category_id: 4>, #<Book id: 15, title: "Pocket Reference JavaScript", description: "erfveafv", author: "fvsfv", created_at: "2018-07-22 20:29:01", updated_at: "2018-07-22 20:29:01", user_id: 2, category_id: 1>, #<Book id: 14, title: "A Game of Thrones: The Illustrated Edition: A Song...", description: "ADFVFV", author: "George R. R. Martin", created_at: "2018-07-22 17:06:24", updated_at: "2018-07-22 17:06:24", user_id: 2, category_id: 2>, #<Book id: 13, title: "The Lion The Witch and The Wardrobe", description: "jsfnvjfnv", author: "Someone", created_at: "2018-07-22 15:59:12", updated_at: "2018-07-22 15:59:12", user_id: 2, category_id: 2>, #<Book id: 12, title: "The Rise of The Islamic State ", description: "fdvmsdfkvmfmskdfvm sdfmvsfdmv", author: "Patrick Cockburn", created_at: "2018-07-22 15:56:06", updated_at: "2018-07-22 15:56:06", user_id: 2, category_id: 4>, #<Book id: 11, title: "1984", description: "JSDNFVJFNVSFNVSLKFJNSJVNJFNVVJFSNDJ", author: "George Orwell", created_at: "2018-07-22 15:22:05", updated_at: "2018-07-22 15:22:05", user_id: 2, category_id: 8>, #<Book id: 10, title: "The Principles of Objected Oriented JavaScript ", description: "Lorem Ipsum is simply dummy text of the printing a...", author: "Walter Isaacson", created_at: "2018-07-22 15:18:05", updated_at: "2018-07-22 15:18:05", user_id: 2, category_id: 1>, #<Book id: 9, title: "Dracula ", description: "Bram Stoker's Dracula is a 1992 American gothic ho...", author: "Bram Stroker", created_at: "2018-07-22 14:48:44", updated_at: "2018-07-22 14:48:44", user_id: 2, category_id: nil>, #<Book id: 8, title: "No Country for Old Men", description: "Cormac McCarthy was born in Rhode Island. He later...", author: "Cormac McCarthy", created_at: "2018-07-22 14:46:23", updated_at: "2018-07-22 14:46:23", user_id: 1, category_id: nil>, #<Book id: 7, title: "Game of Thrones: A Storm of Swords; Steel and Snow...", description: "Megalodon (Carcharocles megalodon), an extinct spe...", author: "George R. R. Martin", created_at: "2018-07-22 14:41:38", updated_at: "2018-07-22 14:41:38", user_id: 1, category_id: nil>]
You have something similar to this in your view:
<%= #books.each do |book| %>
remove the =
<% #books.each do |book| %>
I have trouble for what I thought will be a simple task...
I need to remove the duplicate value of my array for example there are 3 times 2018-02-20 13:48:44 and I only want 1 of those value.
MyTab = [#<Poll id: nil, created_at: "2018-02-20 13:47:15">, #<Poll id: nil, created_at: "2018-02-20 13:48:44">, #<Poll id: nil, created_at: "2018-02-20 13:48:44">, #<Poll id: nil, created_at: "2018-02-20 13:48:44">, #<Poll id: nil, created_at: "2018-02-20 14:25:14">, #<Poll id: nil, created_at: "2018-02-20 14:25:14">, #<Poll id: nil, created_at: "2018-02-20 14:25:14">, #<Poll id: nil, created_at: "2018-02-20 14:32:38">, #<Poll id: nil, created_at: "2018-02-20 14:36:42">, #<Poll id: nil, created_at: "2018-02-20 14:36:42">, #<Poll id: nil, created_at: "2018-02-20 14:40:16">, #<Poll id: nil, created_at: "2018-02-20 15:30:15">, #<Poll id: nil, created_at: "2018-02-20 15:30:15">, #<Poll id: nil, created_at: "2018-03-06 10:24:25">, #<Poll id: nil, created_at: "2018-03-06 10:24:25">]
I already tried this but it does not work :
MyTab.uniq{|x| x.created_at}
Thanks for answers :).
Z.
Edit : Ok i think i get why it 's not working if i try with
a = [["first"], ["second"], ["first"]].uniq
It is working, so i ll change to have my array this way.
Probably slower; but should work :
MyTab.uniq { |x| Time.parse(x.created_at).to_i }
If you can rewrite your query directly from the model i would suggest to do the logic directly in it.
Poll.select(:created_at).where("#{query_conditions}").distinct
If not I suggest #ben's answer.
find_by_sql ["SELECT DISTINCT created_at, FROM your_table_name "]
or
MyTab.flatten!
Note- this will orerwrite MyTab array with unique values
=>s = User.new
=> #<User id: nil, first_name: nil, last_name: nil, email: nil, address: nil, type_of_user: nil, created_at: nil, updated_at: nil>
2.2.0 :015 > o.publisher_id = s
=> #<User id: nil, first_name: nil, last_name: nil, email: nil, address: nil, type_of_user: nil, created_at: nil, updated_at: nil>
2.2.0 :016 > o.publisher_id = 6
=> 6
2.2.0 :017 > s = User.create(:first_name => "Bostnss")
(0.2ms) BEGIN
(0.2ms) ROLLBACK
=> #<User id: nil, first_name: "Bostnss", last_name: nil, email: nil, address: nil, type_of_user: nil, created_at: nil, updated_at: nil>
2.2.0 :018 > o.publisher_id = 5
for that you have to first know that what are the associations that are you using for that you have to apply this
code
in my app i have three models user , order and articles
so for checking the associations i use this
a = Article.new
after insert the data and
use command
a.realod
after that
a.user [the name that i given in the association ]
if it works that means that associations works
I'm having trouble doing the reverse polymorphic association when I have an inherited class. Anyone know what's going on?
ruby-1.9.2-rc2 > Label.first
=> #<Label id: 1, owner_id: 1, owner_type: "Student", name: "Lorem", created_at: "2011-01-23 05:02:29", updated_at: "2011-01-23 05:02:29">
ruby-1.9.2-rc2 > Label.first.owner
=> #<Student id: 1, email: "alice1#example.com", ..., avatar_updated_at: nil>
ruby-1.9.2-rc2 > Label.first.owner.labels
=> []
class Student < User
has_many :labels, :as => :owner
class Label < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
Note: just for good measure (not that this should be any different, but just in case...)
User.find(1).labels
=> []
Also
l = Label.find(4)
=> #<Label id: 4, owner_id: 2, owner_type: "Student", name: "sit", created_at: "2011-01-23 05:02:29", updated_at: "2011-01-23 05:02:29">
ruby-1.9.2-rc2 > l.owner_type = "User"
=> "User"
ruby-1.9.2-rc2 > l.save
=> true
ruby-1.9.2-rc2 > Student.find(2).labels
=> [#<Label id: 4, owner_id: 2, owner_type: "User", name: "sit", created_at: "2011-01-23 05:02:29", updated_at: "2011-01-23 07:13:37">]
Still not sure why it was failing (well, I can guess, given the Student/User dichotomy), but for future reference this hack works:
has_many :labels, :conditions => ["owner_type = ?", "Student"],
:foreign_key => "owner_id"