I have two tables, Table A is the parent, Table B is the child. Both tables have the column "order_number".
I want to find the value in the "order_number" column in Table A, that matches the value of the "order_number" column in Table B, then I'll save the "id" of the Table A record in another column of the Table B record.
I'm having trouble coming up with an activerecord query to find the matching column value. How should I do that?
Activerecord query to find the matching column value:
I think this should do it:
TableA.where(order_number: TableB.pluck(:order_number))
Then you can loop over it to update the tableB column for TableA id like below(assuming its called table_a_id)
tablea_records = TableA.where(order_number: TableB.pluck(:order_number))
tablea_records.each do |a|
b.where(order_number:a.order_number).update_all(table_a_id: a.id)
end
Related
I want to join two tables via ID. The first table has all the IDs and a column with the column names from the other table. I did this with a case statement and if worked but I have like 50 differnet column names and more to come so I don't want to use the case statement. What can I do instead? I'm using Snowflake, this is what I have:
Table A:
tableA
Table B:
tableB
create table statements:
CREATE OR REPLACE TABLE
TABLE_A
(id INTEGER,
column_name VARCHAR);
INSERT INTO
test.TABLE_A
(id,column_name)
VALUES
(1,'column_1'),
(1,'column_2'),
(2,'column_2');
---------------------
CREATE OR REPLACE TABLE
TABLE_B
(id INTEGER,
column_1 VARCHAR,
column_2 VARCHAR);
INSERT INTO
TABLE_B
(id,column_1,column_2)
VALUES
(1,'value_1_1','value_1_2'),
(2,'value_2_1','value_2_2');
This is what I did but as I said this is bad for maintainance:
SELECT
a.ID,
a.COLUMN_NAME,
CASE
WHEN a.column_name = 'column_1'
THEN b.column_1
WHEN a.column_name = 'column_2'
THEN b.column_2
ELSE ''
END AS result
FROM
TABLE_A a
JOIN
TABLE_B b
ON
a.id = b.id
This gives me the following result:
result
How can I get this result without using the case statement or having to type the strings into any condition?
The following code returns the table-names How do I get the individual column names within the table?
ActiveRecord::Base.connection.tables
#=> ["products","listings"]
How do I get: Products.< all columns >
In Rails, the active record class of a table name is the singular form, and capitalized.
So to get the columns for the products table, you would use:
Product.column_names
This provides an array of all the column names for the table represented by the ActiveRecord object, Product.
I have a join table that has a through association between two other tables. This table has its own rails model representation and some fields that are not the the id's of the other tables. Is it possible to update a record from the join table or should I consider creating a unique primary key?
If I try to "update_attributes" on the record I get the following error...
Unknown column 'join_table.' in 'where clause': UPDATE `join_table` SET `join_table_attribute` = 1 WHERE `join_table`.`` IS NULL
You can use update_all for that, e.g.:
JoinModel.update_all('join_table_attribute = 1', 'join1_id = 42, join2_id = 24')
In Rails 3, how do i select rows based on unique column values, i need to get all the columns for eg:
SELECT COUNT(DISTINCT date) FROM records
This only returns date column, but i want all the columns (name, date , age , created_at) columns not just the date.
Thanks for your help
The issue here is that, by definition, there may be multiple records with the same date. It requires logic in the user space to determine which of the multiple records with the unique date to use. Here's some code to get those rows:
Record.select("distinct date").each do |record|
records = Record.find_by_date record.date
puts records.count # do something with the records
end
If what you're really after is uniqueness among multiple columns, list all the relevant columns in the distinct query:
Record.select("distinct date, name, age, created_at").each do |record|
puts record.date
puts record.name
puts record.age
puts record.created_at
# ``record'' still represents multiple possible records
end
The fact that you are using distinct means that each "row" returned actually represents n rows, so the DB doesn't know which of the n rows to pull the remaining columns from. That's why it only returns the columns used in distinct. It can do no other...
I think this will help you
Model.find(:all, :select => 'DISTINCT name, date, age, created_at')
Please use it and let me know.
Model.group(:column)
For your case:
Record.group(:date)
This will return all your columns with no "date" repetitions.
For rails 3.2 and higher, Model.select('DISTINCT name, date, age, created_at')
In two tables mapped to ActiveRecord with unknown number of identical columns, e.g.:
Table A Table B
--------- ---------
id id
name name
age email
email is_member
How can I (elegantly) copy all identical attributes from a record of Table A to a record of Table B, except the id attribute?
For the example tables above, name and email fields should be copied.
Try this:
Get intersection of the columns between TableA and TableB
columns = (TableA.column_names & TableB.column_names) - ["id"]
Now iterate through TableA rows and create the TableB rows.
TableB.create( TableA.all(:select => columns.join(",") ).map(&:attributes) )
Edit: Copying one record:
table_a_record = TableA.first(:select => columns.join(","), :conditions => [...])
TableB.create( table_a_record.attributes)
Migt consider using a union function on the acitverecord attributes hash between the 2 tables. It's not a complete answer but may help