How to obtain or query the description column of the table schema?
Currently:
si_table_name = params[:rid]
#si_field_names = Array.new
si_cols = ActiveRecord::Base.connection.columns(si_table_name, "#{name} Columns")
si_cols.each do |c|
#si_field_names << "#{c.name}:#{c.type}" <---------------
end
Goal: (this example doesn't work... looking for the correct way to query this)
si_table_name = params[:rid]
#si_field_names = Array.new
si_cols = ActiveRecord::Base.connection.columns(si_table_name, "#{name} Columns")
si_cols.each do |c|
#si_field_names << "#{c.name}:#{c.type}:#{c.description}" <---------------
end
Not sure what you mean by 'description'.
In any case, the table metadata can be queried using the information schema.
See
http://dev.mysql.com/doc/refman/5.6/en/columns-table.html
and in particular the table information_schema.columns, column COLUMN_COMMENT.
Related
I am having trouble just trying to pull data out of my table. I just want to pull the integer value from column Diff and add/subtract numbers to it.
Once I am done adding/subtracting, I want to update each row with the new value"
My Table chart for "users" in ruby
This is my code as of now
require 'date'
require 'mysql2'
require 'time'
def test()
connect = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "rubydb")
result = connect.query("SELECT * FROM users where Status='CheckOut'")
if result.count > 0
result.each do |row|
stored_diff = connect.query("SELECT * FROM users WHERE Diff")
#stored_diff = stored_diff.to_s
puts stored_diff
end
end
end
test()
I am sure the code in the hashtag does not work since I am getting like #Mysql2::Result:0x000000000004863248 etc. Can anyone help me with this?
I have no knowledge of ruby but I'll show you the steps to achieve what you are trying based on this and this.
Get User Ids and the Diff numbers.
SELECT `Id`, `Diff` FROM users where `Status`='CheckOut'
Iterate the result.
result.each do |row|
Assign Diff and Id into variables.
usrId = #{row['Id']};
diffCal = #{row['Diff']};
Do your calculations to diffCal variable.
Execute the UPDATE query.
UPDATE `users` SET `Diff` = '#{diffCal}' WHERE `Id` = '#{usrId}'
Trying to write rake task that contains a query that will group by one value on a join table and then sum another column. I'd like to do it using the query interface. Purpose of this task is to find the videos that have been the most popular over the last 5 days.
In pertinent part:
course_ids = Course.where(school_id: priority_schools).pluck(:id)
sections = Section.where(course_id: course_ids)
sections.each do |section|
users = section.users.select {|user| user.time_watched > 0}
user_ids = []
users.each { |user| user_ids << user.id }
user_videos = UserVideo.group(:video_id).
select(:id, :video_id, :time_watched).
where("created_at > ?", Date.today - 5.days).
where(user_id: user_ids).sum(:time_watched)
p "user_videos: #{user_videos.inspect}"
end
Any suggestions for the how / the best way to write this query?
I have the following code and would like to convert the request into a mysql query. Right now I achieve the desired result using a manual .select (array method) on the data. This should be possibile with a single query (correct me if I am wrong).
Current code:
def self.active_companies(zip_code = nil)
if !zip_code
query = Company.locatable.not_deleted
else
query = Company.locatable.not_deleted.where("zip_code = ?", zip_code)
end
query.select do |company|
company.company_active?
end
end
# Check if the company can be considered as active
def company_active?(min_orders = 5, last_order_days_ago = 15)
if orders.count >= min_orders &&
orders.last.created_at >= last_order_days_ago.days.ago &&
active
return true
else
return false
end
end
Explanation:
I want to find out which companies are active. We have a company model and an orders model.
Data:
Company:
active
orders (associated orders)
Orders:
created_at
I don't know if it is possible to make the company_active? predicate a single SQL query, but I can offer an alternative:
If you do:
query = Company.locatable.not_deleted.includes(:orders)
All of the relevant orders will be loaded into the memory for future processing.
This will eliminate all the queries except for 2:
One to get the companies, and one to get all their associated orders.
How can i fetch all the table name and row count for the specific table from the specific database ?
Result
Table Name , Row Count , Table Size(MB)
---------------------------------------
table_1 , 10 , 2.45
table_2 , 20 , 4.00
ActiveRecord::Base.connection.tables.each do |table|
h = ActiveRecord::Base.connection.execute("SHOW TABLE STATUS LIKE '#{table}'").fetch_hash
puts "#{h['Name']} has #{h['Rows']} rows with size: #{h['Data_length']}"
end
The question is tagged mysql but you can do it in a DB-agnostic manner via ORM.
class DatabaseReport
def entry_counts
table_model_names.map do |model_name|
entity = model_name.constantize rescue nil
next if entity.nil?
{ entity.to_s => entity.count }
end.compact
end
private
def table_model_names
ActiveRecord::Base.connection.tables.map(&:singularize).map(&:camelize)
end
end
Note that this will skip tables for which you don't have an object mapping such as meta tables like ar_internal_metadata or schema_migrations. It also cannot infer scoped models (but could be extended to do so). E.g. with Delayed::Job I do this:
def table_model_names
ActiveRecord::Base.connection.tables.map(&:singularize).map(&:camelize) + ["Delayed::Job"]
end
I came up with my own version which is also db agnostic.
As it uses the descendants directly it also handles any tables where the table_name is different to the model name.
The rescue nil exists for cases when you have the class that inherits from ActiveRecord but for some reason don't have a table associated with it. It does give data for STI classes and the parent class.
my_models = ActiveRecord::Base.descendants
results = my_models.inject({}) do |result, model|
result[model.name] = model.count rescue nil
result
end
#temp_table = []
ActiveRecord::Base.connection.tables.each do |table|
count = ActiveRecord::Base.connection.execute("SELECT COUNT(*) as count FROM #{table}").fetch_hash['count']
size = ActiveRecord::Base.connection.execute("SHOW TABLE STATUS LIKE '#{table}'").fetch_hash
#temp_table << {:table_name => table,
:records => count.to_i,
:size_of_table => ((BigDecimal(size['Data_length']) + BigDecimal(size['Index_length']))/1024/1024).round(2)
}
end
end
Exporting some data from mysql to a csv file using FasterCSV. I'd like the columns in the outputted CSV to be in the same order as the select statement in my query.
Example:
rows = Data.find(
:all,
:select=>'name, age, height, weight'
)
headers = rows[0].attributes.keys
FasterCSV.generate do |csv|
csv << headers
rows.each do |r|
csv << r.attributes.values
end
end
CSV Output:
height,weight,name,age
74,212,bob,23
70,201,fred,24
.
.
.
I want the CSV columns in the same order as my select statement. Obviously the attributes method is not going to work. Any ideas on the best way to ensure that the columns in my csv file will be in the same order as the select statement? Got a lot of data and performance is an issue. The select statement is not static. I realize I could loop through column names within the rows.each loop but it seems kinda dirty.
Use the Comma gem:
class Data < ActiveRecord:Base
comma do
name
age
height
weight
end
comma :height_weight do
name
age
height_in_feet
weight
end
end
Now you can generate the CSV as follows:
Data.all(:select => 'name, age, height, weight').to_comma
Data.all(:select => 'name, age, height_in_feet, weight').to_comma(:height_weight)
Edit:
The ActiveRecord finders does not support calculated columns in the resultset, i.e.
data = Data.first(:select => 'name, age, height/12 as height_in_feet, weight')
data.height_in_feet # throws error
You can use select_extra_columns gem if you want to include the calculated columns.
Try this:
def export_to_csv (rows, col_names)
col_names = col_names.split(",") if col_names.is_a?(String)
FasterCSV.generate do |csv|
# header row
csv << col_names
# data rows
rows.each do |row|
csv << col_names.collect{|name| row.send(name)}
end
end
end
cols = 'name, age, height, weight'
rows = Data.find(:all, :select=> cols)
csv = export_to_csv(rows, cols)