Rails pins: How does the pin id work? - ruby-on-rails

Using Ruby on Rails I have generated a pins controller and would like to understand how the pin id works after destroying a pin.
The first pin I created has a pin id of 1, the second has a pin id of 2. After destroying the second pin and creating a new one, the last one I created has a pin id of 3.
Why doesn't the pin created after destroying the second pin have an id of 2? Is there a way to set it to that?

The ids of the database records are usually auto incremented if unless specified. That's why, everytime you delete a record and then create a new record, then the new record gets a next number as its id.
Yes, you can update the record if you want to set the id to something else.
For example, you have a Pin model which has id and name attributes.
You can create a pin like this:
pin = Pin.create(name: 'Pin1')
So, it will get id = 1. But, if you destroy this pin object and re-create another one in a similar fashion:
pin = Pin.create(name: 'Pin1')
it will get id = 2 and so on.
But, you can update it's id attribute if you want like this (Although, in real-life application you don't want to do that!):
pin.id = 1
pin.save
Now, it's id is 1 again.
So, theoretically it's possible, but you should not modify the database record IDs manually in a real-life application.

Related

In a PostgreSQL/Rails app, what decides how the 'id' field is allocated when 'create' method is called?

I have a model called "Post". Because of reasons, I had to create Posts with specific id values. When I run Post.last, it gives me the Post with the highest id value. However, when I run Post.create(), it tries to create a Post with an id greater by 1 than the id of the last Post created without a specified id.
The number of Posts in my database created without a specified id has now caught up to the id of the first Post whose id I specified. This results in an error, because the Post that Rails tries to create would have an id value that's already taken.
Is it possible to somehow override id allocation, to make it go by the value of Post.last.id?
The IDs are generated by a Postgres sequence. Rails will have generated this for you as part of a rails generate model Post, and if you went with all the defaults, your table is called posts and the sequence should be called posts_id_seq.
You need to reset the sequence to the next value after the ID you've already used.
If the next value you want to use is, for example, 5000, something like
ALTER SEQUENCE posts_id_seq RESTART WITH 5000;

Sql query for deleting a member by email in Umbraco8

Does anyone know of any query to delete a member in Umbraco 8 using sql. I had created a member which is corrupt now and creating errors in backoffice, so i want to delete it
I assigned the wrong email address to a new user, so I recreated the user with the correct email address and ended up with a disabled user which never is going to be used. I noticed that you can only "disable" users in the Umbraco back office.
I managed to delete the user via some SQL statements.
But because of some foreign keys, it took some extra steps.
1: Look up all users and thus their respective ID
SELECT * FROM [yourDBname].[dbo].[umbracoUser]
selection umbracoUser
For example, you want to delete the user with id 4. In your case the corrupted member.
2: Delete the user with the respective id from the umbracoUserLogin table
DELETE FROM [yourDBname].[dbo].[umbracoUserLogin] WHERE userId = 4
3: Do the same thing for the umbracoUser2UserGroup
DELETE FROM [yourDBname].[dbo].[umbracoUser2UserGroup] WHERE userId = 1
4: Lastly delete the user from the umbracoUser table
DELETE FROM [yourDBname].[dbo].[umbracoUser] WHERE id = 4
Because of the foreign keys, you should delete user rows in this order ([umbracoUserLogin] -> [umbracoUser2UserGroup] -> [umbracoUser]) otherwise, you get an error that you can't delete the row because of the foreign key.

How to allow only one user to edit a Parse object?

I'm creating an app that allows co-workers to upload shifts (objects) to Parse that they need covered. I save all the shifts to a "Shifts" class (in the data browser). I want to restrict the ALC so that only the person who uploaded the shift/object can edit it (everyone can "read" it). Is there a way that I can create/upload that programmatically (rather than having to go into the Data Browser and add it manually)?
You can create a relationship between your Shifts and your existing _User table. If you add a shift, you can create a relationship to the _User table. Than, if you retrieve the info, you can check if the user who reads the record is also the one who created it by comparing the active PFUser.currentUser with the user from your shift.
//Initial safe
var user = PFUser.currentUser
var relation = user.relationForKey("shifts")
relation.addObject(yourShiftObject)
user.saveInBackground()
You can set the ACL on objects when you create them. The best would be to define a default ACL that allows the creating user to read and write the object while still allowing public read access.
Example taken from the documentation
var defaultACL = PFACL.ACL()
// Optionally enable public read access while disabling public write access.
defaultACL.setPublicReadAccess(true)
PFACL.setDefaultACL(defaultACL, withAccessForCurrentUser:true)
https://www.parse.com/docs/ios_guide#security-recommendations/iOS

convert existing record to new record automatically

I am wondering if, in some hidden corner of the API I haven't yet run into, if there is a way to clone an existing record into a new one, so when saved it will have a new id assigned?
This is intended to be used on an event site I am writing, which will allow people to import from previous years, but copying it will allow updating the event description with new content.
You can use dup method for this.
Given object user1 of model User, you can do:
user2 = user1.dup
user2.save
Doing user2 = user1.dup clones user1 into user2, and user2 has no id, created_at and updated_at values assigned and it is treated as a new record.

After updating the record set the Screen must update

In informix 4gl(genero 4JS) how can I refresh the screen after updating the record set in the database? Right now I can only update the record but the screen is not updating. I think I am missing something. The following set is working fine. after updating I need to refresh the screen with new status.
update person set
person = m_folder.person,
date = m_folder.date,
time = m_folder.time,
status = m_folder.status,
userid = m_folder.userid,
where rowid = g_rowid
After you've updated the database, your program must redisplay the data (possibly after fetching the updated values). I4GL changes the screen when you DISPLAY to it (or INPUT from it); it changes the database when you execute SQL; it never automatically does one because you've done the other (which is actually good; it gives you control over everything).
Try the CALL ui.Interface.Refresh() instruction.
If it doesn't work then Jonathan Leffler is right: you have to use some dialog-type instruction (DISPLAY or INPUT) to display it, like DISPLAY BY NAME m_folder.*

Resources