How to use autoincrement primary keys with CL-SQL? - clsql

I am using CL-SQL with SQLite backend, and I can't quite get autoincremented primary keys to work. I declared a slot like (in def-view-class):
((id :accessor d-id :db-constraints :primary-key :type integer :db-type "INTEGER")
But if I create the class, the field is not updated, not even when I call update-records-from-instance, and if I call update-instance-from-records, it will get updated wrong. Is there a way to use autoincremented fields CL-SQL at all?

I think this might be caused by a bug. I've sent a patch to the clsql mailing list which fixes the problem for me:

Related

Ruby on Rails - using a block parameter as a method call

I'm having trouble with a little Ruby on Rails I'm building and need some help.
I have a Table with 20+ Columns and a corresponding XML File which can be parsed as some sort of hash with a gem. Every key would be mapped to a column and every value would be a data record in said column.
The way I access a specific value in the already parsed XML file is:
filename["crs","inputkeyhere"]
which returns the value, for example "52" or whatever.
What I am trying to do is upload the file, parse it with the gem and give each column the corresponding value.
My table (or model) is called "Attributeset" and I already know how I can access every column:
#attributeset = Attributeset.new
#attributeset.attributes.keys
So my thought process was:
Iterate over all the keys
Pass every key into a block called |a|
Use the rails possibilty to set attributes by calling the corresponding #attributeset.
Set colum attribute to the corresponding xml key
So my code would go something like this:
#attributeset.attributes.keys.each do |a|
#attributeset.a=filename["crs",a]
end
But my problem is, that ruby thinks ".a" is a method and apparently does not evaluate "a" to the block parameter.
I've read through lambdas and procs and whatnot but didn't really understand how they could work for my specific situation.
Coming from bash scripting maybe my thinking might be wrong but I thought that the .a might get evaluated.
I know I can run the block with yield, but this only works in methods as far as I know..
Any help is appreciated.
Thanks and stay healthy,
Alex
Thanks for the input!
I wanted to make it as clean as possible, and not using any temporary hashes to pass arguments.
I've found the method
write_attribute
which can be used like this:
#attributeset.write_attribute(a, xmp["crs",a])
worked perfectly for me.
You can use []= method to set values dynamically:
#attributeset.attribute_names.each do |attribute|
#attributeset[attribute] = filename["crs", attribute]
end

Rails 4 update_all and set value from another field

I need to do some bulk updates in some models and set value of a field as value of another field.
Right now I can do that with raw sql like this:
ActiveRecord::Base.connection.execute("UPDATE `deleted_contents` SET `deleted_contents`.`original_id` = `deleted_contents`.`id` WHERE `deleted_contents`.`original_id` is NULL")
This is working fine, however I need to do this using ActiveRecord query interface due to many reasons.
I tried:
DeletedContent.where(original_id: nil).update_all(original_id: value_of_id_column)
For value_of_id_column I tried :id, self.id, id, etc, nothing works. What should I set for value_of_id_column to get the original query generated by rails? Is this possible, or using the raw sql is the only solution?
Also I do not want to iterate over each record and update. This is not a valid solution for me:
DeletedContent.where(original_id: nil).each do |deleted_content|
update_each_record
end
I'm pretty sure you cannot obtain that query by passing a hash to update_all.
The closest to what you want to obtain would be:
DeletedContent.where(original_id: nil).update_all("original_id = id")

Rails 4 + Mongoid 4: Model.only("field").to_a not working as before

I just started a new project in Rails 4 and Mongoid 4 beta and an old behaviour that I used a lot in Mongoid 3 is not working anymore.
Before I used to write Model.only("field").to_a and I would get an array with id and field, all other fields were set to null.
If I try to do this in Mongoid 4 I get: (Object doesn't support #inspect)
Model.only("field").map {|e| e.field} is working although not as before. The id is not included anymore, I get ActiveModel::MissingAttributeError if I try to access the id.
I know I can use Model.pluck("field"), this will not return an array of documents though.
Are these changes real or am I missing something?
EDIT:
As I'm writing this I tried including the id and it's working. ie. Model.only("id", "field").to_a working as before, but my question is still valid. Do I have to include the id now in order to get an array of documents?
This is a new behaviour on Mongoid 4. As you said you can add the "id" field to the only method, and it should work. You also can use the pluck method to get and array of fields you want. Something like:
Model.all.pluck("id", "field"). As you said, You already knew about pluck, and thats the way to go..
cheers.

How to use find method for composite primary keys in ruby on rails?

I have a table called SubElement.
It has two primary keys : Element_Code, Sub_Element_Code
In my view, when i try to do something like the following
<%
sub_element_model = Condition::SubElement
sub_element = #sub_element_code.nil? ? sub_element_model.new : sub_element_model.find(#sub_element_code)
if sub_element.persisted?
#element_code = sub_element.Element_Code
f.object.Sub_Element_Code = #sub_element_code
end
%>
I got an error after selected the sub_element_value like
["4"]: Incorrect number of primary keys for Condition::SubElement: [:Element_Code, :Sub_Element_Code]
How can i use the find method for two composite primary keys.
Update:
In the form, I have element_code field, sub_element_code and material. But everything should visible once the parent selected. With the help of some javascript I try to finish this. The Main problem of what I could not explain in detail is the form fields are creating by some helper file. It's a very large file and i cant change that. So I am looking for the alternative solution to change the find method for the two composite primary keys to get the value.
Not exactly sure what you're trying to do with the code above but you could use where:
sub_element_model.where(first_id: 3, second_id: 5).first
Your code doesn't look very conventional-ish however, perhaps if you described what you're trying to do SOers could help you come up with a neater solution :)
I myself never try using a multiple attribute primary key, but I believe that ActiveRecord is telling you that you are not using the correct number of primary keys in your find search.
Try something like
Model.find([first_part_of_primary_key, second_part_of_primary_key])
You should not be using find (which gets a single record by primary key - which you don't completely have), but rather where (which gets you a collection of records by any field(s) you want):
sub_element = #sub_element_code ?
SubElement.where(sub_element_code: #sub_element_code).first :
SubElement.new

Rails - Data Migration vs. value in DB

I have a question about where values in dropdowns are coming from:
I have a migration that set up the original table with some initial values:
add_column :contracts, :signature_status_id, :integer
# lookup data
sig = SignatureStatus.new(:name => "Delivered")
sig.save!
sig = SignatureStatus.new(:name => "Signed")
sig.save!
I have a table called signature_statuses that contains the updated values:
id, name
1, 'Delivered; awaiting signature'
2, 'Delivered; awaiting full execution'
3, 'Terms being negotiated'
4, 'Fully executed and filed'
I have a form that contains the code to pull out the signature status:
<%= collection_select(:contract, :signature_status_id, #signature_statuses, :id, :name) %>
The collection select is pulling in "Signed" and "Delivered" when I want it to be from the DB. How do I make it do that.
Note: I think that the data was edited manually rather than a migration, but I'm not sure. I also searched the code for "signed" and "delivered", but the only place it shows up is in the migration.
I'm just wondering how are you getting that list of values in the signature_statuses table? Are you querying your development database? Is your application running in development mode? Is the database.yml file setup correctly to point to your development database?
Also can you post the controller code that populates the #signature_statuses variable.
A little more info and I'm sure people will be able to help.
Hmmm, this is a bit odd, but i suspect the following: there might be a method called name inside your signature_status model which is overriding the default one and which returns yes and no.
The key to debugging this is to look where
#signature_statuses
is being set in the controller. If it's pulling from the database, then that is what is in the database. I wonder if there is more than one database involved, where your migration updated the development database, but you are running the query against production (or something like that).
It turns out I needed to run "rake db" and that fixed it.

Resources