Sequel Migration update with a row's ID - ruby

How can you run a Sequel migration that updates a newly added column with a value from the row?
The Sequel documentation shows how you can update the column with a static value:
self[:artists].update(:location=>'Sacramento')
What I need to do is update the new column with the value of the ID column:
something like:
self[:artists].each |artist| do
artist.update(:location => artist[:id])
end
But the above doesn't work and I have been unable to figure out how to get it to go.
Thanks!

artist in your loop is a Hash, so you are calling Hash#update, which just updates the Hash instance, it doesn't modify the database. That's why your loop doesn't appear to do anything.
I could explain how to make the loop work (using all instead of each and updating a dataset restricted to the matching primary key value), but since you are just assigning the value of one column to the value of another column for all rows, you can just do:
self[:artists].update(:location=>:id)

if you need update all rows of a table, because it is a new column that need be populate
artists = DB[:artists]
artists.update(:column_name => 'new value')
or if you need, update only a unique row into your migration file you can:
artists = DB[:artists]
artists.where(:id => 1).update(:column_name1 => 'new value1', :column_name2 => "other")

Related

I get duplicate entries with the attach method in Laravel 5. Not sure what the problem is

So I'm trying to attach id's with some meta data to a pivot table in Laravel 5.
For some reason, I get the two inserts where there should be one, and the wrong ID's being inserted the second time round.
I'm not sure if there is something I might be missing here.
This is the code:
$match_values = array(
'dataId' => $result->id,
'dataMetaId' => $the_meta->id
);
$result->campaignDataMeta()->attach($match_values, [
'meta_value' => $value
]);
The database structure consists of a main campaignData table for email campaigns, a campaignDataMeta table (id, timestamps, name) for email meta data names, and a lookup table campaignDataMatches (id, campaignDataId, campaignDataMetaId, meta_value).
In campaignDataMatches I get the campaignDataId value sometimes being inserted into the campaignDataMeta column.
I've solved the problem.
Apparently had to add the relevant ID (in this case the dataMetaId) within the attach parameter.
Like this:
$result->dataMeta()->attach([$data_meta_id => [
'meta_value' => $value
]]);
Check the database columns primary maybe the dataId and metaId are both primary.

ruby sequel gem - how to query arrays with the pg_array extension

I am using the pg_array extension and sequel version 4.1.1.
I have added the extension like this:
Sequel::Database.extension :pg_array
I have created a column like this:
alter_table :emails do
add_column :references, "text[]", null: true
end
I can load and retrieve arrays into a postgress array column, just like working with normal arrays.
What is not clear from the above link is how do I execute a query based on the values in this array column.
For example, if one row in the emails table contained these values in the references column:
references
--------------------------------------------------------------------
{}
{5363f773bccf9_32123fe75c45e6f090953#Pauls-MacBook-Pro.local.mail}
How can I query the emails table to find a row that contains a references array value of the above value:
Email.where(references: ????)
Use the pg_array_ops extension:
Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('5363f773bccf9_32123fe75c45e6f090953#Pauls-MacBook-Pro.local.mail'))
Have you tried?
ref = '5363f773bccf9'
emails = Email.arel_table
Email.where( emails[ :references ].matches( "%#{ref}%" ))

Get full rows based on single column distinct

Actually I want to get the full table but it should be based on Doc_Type==distinct
Means it should only pick the records from table that has unique Doc_Type. I have tried with following but it returns me a single column into tolist() but I want to get full table. How can I do it?
var data = DB.tblDocumentTypes.Select(m => m.Doc_Type).Distinct().ToList();
You can use GroupBy
var data = DB.tblDocumentTypes.GroupBy(m => m.Doc_Type).Select(x => x.First());

How to assign values for update_attributes in rails?

I have a noob question about assigning values using update_attributes.
In the exam controller, a new exam record is saved and then an patient record is retrieved which matches some of the new exam fields. This part works fine.
#exam.save
#patient = Patient.joins(:charts).where(:dob => #exam.patient_dob).where(:charts => { :provider_id => #exam.provider_id, :patient_mrn => #exam.patient_mrn })
Then i try to update the new #exam record with a value from the #patient record using the following which crashes and burns......
#exam.update_attributes(:patient_id, #patient.id)
How have i gone so far astray?
You're updating a single value there, not multiple values, so update_attribute would be more appropriate. update_attributes takes a hash of values to update.
See http://apidock.com/rails/ActiveRecord/Base/update_attribute and http://apidock.com/rails/ActiveRecord/Persistence/update_attributes

Ruby Rails - save vs update where method

I am trying to make a inventory system, my table has the index "id" by default as primary key. My inventory number should be unique value to create new entry in the table, if the inventory already exists then it shall update the attributes. How do do this? By making inventory number as primary key? or is there any way to check and update an inventory already exists?
You can have unique field number (for example). Than you can use find_or_create_by_<field_name> dirty method.
#ticket = Ticket.find_or_create_by_number(503)
Updated:
#ticket.attrib = 'new attribute value'
#ticket.save
or
#ticket.update_attribute :attrib, 'new attribute value'
This worked: create_or_update method in rails
my_class = ClassName.find_or_initialize_by_name(name)
my_class.update_attributes({:street_address => self.street_address,.....})

Resources