Select query in Ruby on Rails - ruby-on-rails

I am new to ruby on rails but I am not finding the meaning of this line of code. I saw in the documentation of rails that select will build an array of objects from the database for the scope, converting them into an array and iterating through them using Array#select. Anyway I can’t understand the result of this line of code and on what it consists.
model.legal_storages.select{|storage| storage.send(document_type)==true}.last

model.legal_storages.select { |storage| storage.send(document_type) == true }.last - From the result of the last operation, select only the last element.
| | |
| | --------------------- For each element in model.legal_stores invoke
| | the method that is held by the variable document_type
| | and check if it's equal to true.
| |
| --------- Over the result of the last method,
| call select to filter those elements where
| condition in the block evaluates to true.
|
------------------- Invoke the method legal_stores in model.

Related

Unwrapping a Markdown table with a Pandoc Lua filter

I have a DOCX file with this content:
# Heading
+---------------------+
| Paragraph |
| |
| ## Subheading |
| |
| +-----------------+ |
| | Nested table | |
| +-----------------+ |
+---------------------+
One last paragraph
Here is a sample file.
I want to run it through Pandoc and get this Markdown, with all tables unwrapped:
# Heading
Paragraph
## Subheading
Nested table
One last paragraph
I'm trying to write a Lua filter with walk_block but I have no experience with Lua and not making any progress. Can anyone point me in a helpful direction?
function Table(table)
pandoc.walk_block(table, {
Str = function(el)
-- TODO now what???
end
})
end
The Lua interface to tables is currently rather complex, so it's much simpler to convert the table into a so-called simple table. We can do so with pandoc.utils.to_simple_table. A simple table has a header row (header) and multiple body rows (rows), and we get access to cells by iterating over a row. Each cell is just a Blocks list, which we can collect in an accumulator.
Here's how this looks like:
function Table (tbl)
local simpleTable = pandoc.utils.to_simple_table(tbl)
local blocks = pandoc.Blocks{}
for _, headercell in ipairs(simpleTable.header) do
blocks:extend(headercell)
end
for _, row in ipairs(simpleTable.rows) do
for _, cell in ipairs(row) do
blocks:extend(cell)
end
end
return blocks, false
end
Running that filter should unwrap all tables, leaving just their contents.

Pivot a table using a stored procedure

So I'm trying to pivot a table using a stored procedure in Redshift. The issue is that the result set is dynamic. That means that we'd need to be able to dynamically pivot the table below. This is what I am trying to pivot:
| object_uid | field | value |
|------------|----------|----------|
| post:1 | field_1_a| test |
| post:2 | field_2_a| turtle |
| post:2 | field_2_b| frog |
| post:3 | field_3_a| mountain |
| ...... | ..... | ...... |
|------------|----------|----------|
This would be pivoted into the following:
| object_uid | field_1_a| field_2_a| field_2_b| field_3_a|
|------------|----------|----------|----------|----------|
| post:1 | test | | | |
| post:2 | | turtle | frog | |
| post:3 | | | | mountain |
| ....... | ..... | ....... | ....... | ....... |
|------------|----------|----------|----------|----------|
Essentially I am trying to construct a chained string of column names (the field_* columns) via SELECT LISTAGG statement in the subquery, and trying to interpolate that statement's output in the CREATE TABLE sql statement. Then once the CREATE TABLE sql statement is constructed, the sql statement gets executed via the EXECUTE command.
However, This is not behaving as expected. I am a relative newcomer to Redshift, so I apologize in advance if this is a terrible way to go about pivoting a table from a tall one to a wide one. This is the code that I have so far:
CREATE OR REPLACE PROCEDURE my_proc (
tmp_name INOUT varchar(256)
) AS $$
DECLARE
sql VARCHAR(MAX) := '';
BEGIN
WITH pivot_output AS (
SELECT LISTAGG(temp.output, ', ') WITHIN GROUP (ORDER BY temp.output) AS metadata FROM
(
SELECT DISTINCT
'MAX(IF(cm.metadata = ''' || metadata || ''',cm.field_value,NULL)) AS ' || QUOTE_LITERAL(metadata)
AS output
FROM "content_metadata" cm
WHERE cm."source_uid_type" = 'post'
) AS temp
);
sql = 'CREATE TABLE ' || tmp_name || ' AS SELECT cm.object_uid, ' || pivot_output.metadata || ' FROM content_metadata cm GROUP BY cm.object_uid';
EXECUTE sql;
END;
$$ LANGUAGE plpgsql;
CALL my_proc ('output_table');
I get the following error when trying to execute the above:
The database reported a syntax error: Amazon Invalid operation: syntax error at or near "$1";
A little bit stumped by the error. Does anyone have any clues / suggestions?
I added a working example of emulating the PIVOT FOR syntax in our GitHub repo "Amazon Redshift Utils". https://github.com/awslabs/amazon-redshift-utils/blob/master/src/StoredProcedures/sp_pivot_for.sql
I hope this is useful for you. Let me know if you have any issues with it.

cannot find "for loop" keyword in robot framework

I am currently connecting SQL server to robot framework, so i can read my data table name in robot. and I want to use for loop to check table name, somehow, ":FOR" loop keyword cannot found, but I have installed libraries such as operating-system, collections, string, built-in, diff-library and so on. anyone can help me why i cannot use for loop? any help will be appreciated.
The robot framework users guide has a whole section on how to use the for loop. From that section:
The syntax starts with :FOR, where colon is required to separate the
syntax from normal keywords. The next cell contains the loop variable,
the subsequent cell must have IN, and the final cells contain values
over which to iterate. These values can contain variables, including
list variables.
Here's an example from the user's guide, reformatted to use pipes (for clarity):
*** Test Cases ***
| Example 1
| | :FOR | ${animal} | IN | cat | dog
| | | log | ${animal}
| | | log | 2nd keyword
| | Log | Outside loop
Maybe you are not escaping indented cells; as the Tip in the documentation says. Try writing loops like this:
:FOR ${index} IN RANGE ${start} ${stop}
\ log to console index: ${index}
\ Call a Keyword

Getting filename when using TextDirectoryLoader - weka

I am using the TextDirectoryLoader in weka which takes in as an input a directory which has the training data as files arranged in folders and each folder indicates a class label. I pass the test_example directory name as an argument. The training part is fine.
Example:
+- text_example
|
+- class1
| |
| + file1.txt
| |
| + file2.txt
| |
| ...
|
+- class2
| |
| + another_file1.txt
| |
| + another_file2.txt
| |
| ...
The above illustration borrowed from here
For testing and predicting labels, I create a similar structure.
+- predictor_unknowns
|
+- unknown
| |
| + unknownfile1.txt
| |
| + unknownfile2.txt
| |
| ...
I again pass the director predictor_unknowns as an arguement to TextDirectoryLoader and I can see the predicting is done fine but I am not sure how to print the file name for which the preidiction is happening. I need to print unknownfile1.txt,unknownfile2.txt etc for which the prediction is happening.
Hope the question is clear enough.
In weka, those text files and classes become an Instance and the filenames are not saved in Instance class.
Instead, you can get the text content of that file which got classified.
double pred = 0d;
Instance current = getInstance();
pred = classifier.classifyInstance(current);
System.out.println("\nText: "+current.attribute(0)); // Change index according to your dataset
System.out.println("Class: "+tempInstances.classAttribute().value((int) pred));
In the interest of benefiting others who may have this question, the documentation for the TextDirectoryLoader explains that you can save the filename as an extra attribute.
On the command line, just add the -F flag.
In Java code, you can use the following line (tdl is an instance of TextDirectoryLoader):
tdl.setOutputFilename(true);
As long as you do not run the dataset through any filters, each instance will have a string attribute called "filename". If you are planning to run the dataset through filters, it may be useful to use a FilteredClassifier so that you can still access the filename.

fitnesse: howto load a symbol with a result in query table

In a FitNesse query table, is it possible to load a symbol with the returned results?
example of what I would like to do:
|Query: GetPlayers|
| name | age | ID |
| jones | 36 | $ID1= |
| smith | 27 | $ID2= |
Or alternatively, just have one $ID symbol which is loaded with a collection.
Is this possible?
Unfortunately I believe this is still an unresolved issue in FitNesse. There is a PivotalTracker entry for it, that no one has take one yet: https://www.pivotaltracker.com/story/show/1893214. I've looked at it, but haven't been able to solve it myself.
We currently work around this by having a driver that can do equivalent query. Then we get the value back from the query. It is much more cumbersome, but works for now.
I completely agree that this should be possible. But as far as I know, it has not been fixed yet.
Maybe I don't understand your problem, but this is working fine for me:
|Query: whatever|whatever_param |
|key |value |
|a_key |$symbol= |
|check |$symbol|a_value|
I use Cslim, and the method whatever.query() returns a list of maps that correspond to the keys (the key a_key have the value a_value for this exemple)

Resources