Ruby - Convert SQL unnest to Arel - ruby-on-rails

How can I convert the following query into Arel? I want to define a where condition using arel.
select * from tenants where id in (select a.id::int from tenants t2 cross join unnest(string_to_array(t2.ancestry, '/')) as a(id) where t2.id = 39);
Here is how the table output looks like:
-[ RECORD 1 ]-------------+------------------
id | 39
domain |
subdomain |
name | Child_Teanant_201
login_text |
logo_file_name |
logo_content_type |
logo_file_size |
logo_updated_at |
login_logo_file_name |
login_logo_content_type |
login_logo_file_size |
login_logo_updated_at |
ancestry | 1/35
divisible | t
description | Child_Teanant_201
use_config_for_attributes | f
default_miq_group_id | 51
source_type |
source_id |

Related

How to correct query object to shows data properly in Rails 5

I need some fresh eye to my object query. The idea is to show from db all users which have only one client (belongs_to :user), which are not subscribed.
models
user
has_many :customers
has_many :customers_users
customer
has_many :customers_users
belongs_to :user
customers_user
belongs_to :user
belongs_to :customer
customer_subscription
belongs_to :customer
query
Customer.
joins("LEFT JOIN customer_subscriptions ON customer_subscriptions.customer_id = customers.id").
where(customer_subscriptions: { id: nil }).
joins("RIGHT JOIN customers_users ON customers_users.customer_id = customers.id").
group(:user_id).
having("count(user_id) = ?", 1).
count
But after all I've got only user_id without data such as name, email, nickname etc. I thought the problem is with count at the end but without count I received an error NoMethodError: undefined method 'having' for main:Object
Is it possible to reach these data in one query?
In your case I guess a negative condition in the join would work. So the query would be like:
SELECT * FROM users
INNER JOIN customers
ON customers.user_id = users.id
INNER JOIN customers_subscriptions
ON customers_subscriptions.customer_id != customers.user_id
GROUP BY users.id
HAVING COUNT(users.id) = 1
So having:
Users:
+----+-------+------------+----------------------------+----------------------------+
| id | email | name | created_at | updated_at |
+----+-------+------------+----------------------------+----------------------------+
| 1 | NULL | seb | 2019-04-27 19:16:09.043251 | 2019-04-27 19:16:09.043251 |
| 2 | NULL | sab | 2019-04-27 19:16:09.150315 | 2019-04-27 19:16:09.150315 |
| 3 | NULL | washington | 2019-04-27 19:58:01.737446 | 2019-04-27 19:58:01.737446 |
+----+-------+------------+----------------------------+----------------------------+
Customers:
+----+------------+-------+---------+----------------------------+----------------------------+
| id | name | email | user_id | created_at | updated_at |
+----+------------+-------+---------+----------------------------+----------------------------+
| 1 | Sab | NULL | 1 | 2019-04-27 19:16:09.254955 | 2019-04-27 20:02:47.636143 |
| 2 | Seb | NULL | 2 | 2019-04-27 19:16:09.313268 | 2019-04-27 20:02:55.741603 |
| 3 | Washington | NULL | 3 | 2019-04-27 19:58:22.711897 | 2019-04-27 19:58:45.213720 |
| 4 | Eminem | NULL | 3 | 2019-04-27 19:58:52.820731 | 2019-04-27 20:03:02.465681 |
+----+------------+-------+---------+----------------------------+----------------------------+
Customers Subscriptions:
+----+-------------+----------------------------+----------------------------+
| id | customer_id | created_at | updated_at |
+----+-------------+----------------------------+----------------------------+
| 1 | 1 | 2019-04-27 19:16:10.041788 | 2019-04-27 19:16:10.041788 |
| 2 | 3 | 2019-04-27 20:04:16.464446 | 2019-04-27 20:04:16.464446 |
+----+-------------+----------------------------+----------------------------+
The result should be:
+----+-------+------+----------------------------+----------------------------+----+------+-------+---------+----------------------------+----------------------------+----+-------------+----------------------------+----------------------------+
| id | email | name | created_at | updated_at | id | name | email | user_id | created_at | updated_at | id | customer_id | created_at | updated_at |
+----+-------+------+----------------------------+----------------------------+----+------+-------+---------+----------------------------+----------------------------+----+-------------+----------------------------+----------------------------+
| 1 | NULL | seb | 2019-04-27 19:16:09.043251 | 2019-04-27 19:16:09.043251 | 1 | Sab | NULL | 1 | 2019-04-27 19:16:09.254955 | 2019-04-27 20:02:47.636143 | 2 | 3 | 2019-04-27 20:04:16.464446 | 2019-04-27 20:04:16.464446 |
+----+-------+------+----------------------------+----------------------------+----+------+-------+---------+----------------------------+----------------------------+----+-------------+----------------------------+----------------------------+
So an AR code for that could be:
User.
joins(:customers).
joins('INNER JOIN customers_subscriptions ON customers_subscriptions.customer_id != customers.user_id').
group(:id).
having('COUNT(users.id) = 1')

Rails - How to group_by with many models?

I have this chain of models:
And I have a list of suboptions. I would like to group_by the suboptions by each of the above fathers.
So, it would be divided into Categories, which would be divided into activities, which would be divided into options. Do you see?
How can I do this??
Suboptions.all.group_by(???)
# or
Suboptions.all.order_by(????)
# or ????
Example:
Suboptions
+----+-----------+
| id | option_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 4 |
| 5 | 3 |
+----+-----------+
Options
+----+-------------+
| id | activity_id |
+----+-------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 3 |
+----+-------------+
Activities
+----+-------------+
| id | category_id |
+----+-------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
+----+-------------+
Categories
+----+------+
| id | name |
+----+------+
| 1 | cat1 |
| 2 | cat2 |
+----+------+
then the search should return the suboptions with ids in the following order: [1,5,4,2,3]
( try to visualize it from the category to the suboption )
Depending on the size of the data set, sounds like you want to do a join, followed by an order
Suboption.joins(option: {activity: :category}).order('categories.name')
Will that work for you?

Rails Search returns duplicate

I am doing an inner join (at least I think that's what the code is doing) but my search is returning the same result multiple times. I think I have something wrong with my join.
Tags
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| color | varchar(255) | YES | | NULL | |
| article_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
Articles
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| info | text | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
Each article I have tagged and it returns multiple results based on the tags. So if the article has 3 tags this results in 3 records being returned. Even though each article should only have 1 returned?
class Article < ApplicationRecord
has_many :tags, dependent: :destroy
validates :title, presence: true
def self.search(search)
if search
joins(:tags).where('title LIKE :search OR tags.name LIKE :search', search: "%#{search}%")
else
all
end
end
end
Use .distinct or .group. There is .uniq alias too starting from rails 4.0.2.
Example:
joins(:tags).where('title LIKE :search OR tags.name LIKE :search', search: "%#{search}%").distinct
joins(:tags).where('title LIKE :search OR tags.name LIKE :search', search: "%#{search}%").
group('article_id')

OOP programming method question

I have 2 objects, Area and SurfBreak. Area has many SurfBreaks and a SurfBreak publishes its conditions based on wind,wave,tide info from Area. This bit I've done an it works well:-)
I now have list of forecast data for Area - future changes to Area's attributes.
Whats the best OOP method to show the Surfbreaks conditions using forecast data for Area ?
Many thanks
Andy
----Updated---
Its a rails app
class Spot < ActiveRecord::Base
belongs_to :area
has_many :forecasts, :through => :area
def has_swell
wind = "#{area.swelldir}"
beachstart = "#{breakstr}"
beachend = "#{breakend}"
if ( ((wind.to_i) + 360 - (beachstart.to_i)) % 360 <= ((beachend.to_i) + 360 - (beachstart.to_i)) % 360 )
"#{area.swelldir} Has Incoming swell "
else
"#{area.swelldir} No Swell"
end
end
class Area < ActiveRecord::Base
has_many :spots
has_many :forecasts
class Forecast < ActiveRecord::Base
belongs_to :area
The DB tables are the objects in rails. I've got Area and Spot working nicely but I now want to display forecasts for a spot. This is the bit I'm not sure about.
mysql> desc areas;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| descrip | varchar(255) | YES | | NULL | |
| winddir | int(11) | NO | | NULL | |
| windspd | int(11) | NO | | NULL | |
| swelldir | int(11) | NO | | NULL | |
| swellhgt | float | NO | | NULL | |
| tide | int(11) | NO | | NULL | |
| lat | float | YES | | NULL | |
| lng | float | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
mysql> desc spots;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| descrip | varchar(255) | NO | | NULL | |
| breakstr | int(11) | NO | | NULL | |
| breakend | int(11) | NO | | NULL | |
| offstr | int(11) | NO | | NULL | |
| offend | int(11) | NO | | NULL | |
| besttide | int(11) | NO | | NULL | |
| area_id | int(11) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> desc forecasts;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| forecastdate | datetime | YES | | NULL | |
| area_id | int(11) | NO | | NULL | |
| winddir | int(11) | NO | | NULL | |
| windspd | int(11) | NO | | NULL | |
| swelldir | int(11) | NO | | NULL | |
| swellhgt | float | NO | | NULL | |
| tide | int(11) | NO | | NULL | |
+--------------+----------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
So say an Area has 24 Forecast rows in a DB , one for every hour in the future. In my app
what is the best way to output a spots forecast conditions. Without changing the relevant values in the Area as Areas hold the current conditions. I could just pull all the forecast data into an array an loop through it changing the Area object data, but this doesn't seem very OOP to me ?
As output I'm after something like
Current Spot Details (Using spot methods on Area attributes)
xxx
Forecast Details for this spot (Using spot methods on Forecast attributes )
Hour 1 xxx
Hour 2 xxx
Hour 3 xxx
..
Sorry if this is not very well explained.
Regards
Andy
Your class Area sounds like it is doing too many things, and it is changing for different reasons. Separate it out so the Area has a list of WeatherData or something, so your forecasting code can iterate through the WeatherData without Area having to change. Your WeatherData object can include a flag saying whether it's real data or a forecast.
Class Area{
Wind wind;
Wave wave;
Tide tide;
}
Class SurfBreak extends Area{
//some SurfBreaks' field
public ForecastDetail getForecastDetail(){
//operate directly onwind wave tide fields and calculate
}
}
You haven't explained exactly how you developed first part of the problem (relations between Area and SurfBreaks), but, I would consider using of Observer design pattern here. So SurfBreaks would be Observers of Area changes.

Forum name not displaying

Not sure why.
My model defines the object forum
def index
#forum = Forum.find(params[:forum_id])
But in my view it won't display the forum name. This code just comes up with "Forum : "
<h2>Forum : <%- #forum.name -%></h2>
The forum database table exists, and has an object that should be displayed!
mysql> desc forums;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| topics_count | int(11) | NO | | 0 | |
+--------------+--------------+------+-----+---------+----------------+
The terminal log doesn't show any errors, so it has searched the database correctly and returned a value - it's just not showing it! Grr....
Parameters: {"forum_id"=>"2"}
Forum Columns (3.8ms) SHOW FIELDS FROM `forums`
Forum Load (0.7ms) SELECT * FROM `forums` WHERE (`forums`.`id` = 2)
Topic Load (0.7ms) SELECT * FROM `topics` ORDER BY updated_at DESC LIMIT 0, 30
Help?!?
You need an =, like this:
Forum : <%= #forum.name %>
Get rid of the minuses at either end as well. That should do it!

Resources