Deleted unique index constraint still active - ruby-on-rails

I have a model that has a status field (which can be either draft, active or archived) and a code field.
With the unique index that I recently deleted, it made a constraint that wouldn't allow two records to share the same code. I needed to update that unique index to allow the reuse of codes if the record that shares that code has an archived status.
But after deleting the old unique index and creating a new unique index with the new constraint I'm still unable to create new records that use codes used by records that have a status of archived that were created before the update to the unique index.
I'm not very familiar with unique indexes and wasn't sure if there was something more I needed to do to remove that constraint. I don't want to delete any of the old records.
edit:
I deleted the index on the table through a migration and created the new unique index from the same migration.

Related

Rails deleting records is leaving null columns

When I delete record like this:
ScheduledTask.destroy(params[:scheduled_id])
Instead of erasing the record, it sets all its values to null. How to delete the record completely?
ScheduledTask is the name of your entire class rather than a record. At the moment you're telling the entire class to delete the supplied params.
You need to find the record you want to delete, and then call delete on that:
task = ScheduledTask.find(params[:scheduled_id]
task.destroy

How to prevent inserting rows with no two rows will have same (duplicates) value

When multiple users are accessing the application,the application should not allow to insert duplicate columns(Eg: Name column in users table where name is unique).
How I am doing this is, in the button click event I am checking whether the name exists in db. this shows validation error if it is exists(duplicate);This works perfectly when single user accessing the app.
But, when 2 users concurrently inserting a row(user) with same column name it is allowing to inserting records with same name, because by the time first user's request hit the db for checking whether the name already exists, second user's record(with duplicate name) is not yet inserted.
so, no validation is showing.
How to fix this issue in nhibernate sessions?.Can I use locking?
please advice.
You should enforce this with a unique constraint on the database. Then when committing your insert transaction you should catch the error of the constraint violation. That's one of the things a database is great at doing.

How do I add a column that points to another table in a Rails database?

Let's say I currently have a table titled "Report" and within Report, there are a bunch of strings, integers, timestamps, etc.
Now, I want to somehow be able to see who is currently working on a certain report - all I need are names, but the key is it could be more than one name so just adding a string column to the Report table wouldn't work. Plus, the names would keep changing - at one point, "Steve" and "John" could be working on a report, but once "Steve" stops, I need to be able to get rid of his name from the table.
I have a feeling that the correct way to do this is by:
creating a new table titled "Viewers" and having just one string field (for the person's name) in there. I'd add rows to this table when a new viewer is detected.
linking to indices within the Viewers table from the Report table. When a person stops viewing the report, the respective index is destroyed.
I think that makes sense logically, but I'm hesitant to implement this without being sure that this is the way to go. Any help/feedback/suggestions would be appreciated!
Edit:
I created a model for my join table and it auto-generated a migration and added an integer "viewer_id" and an integer "report_id" like Vimsha suggested below. However, I can't correctly implement the join tables in my code for some reason - when I want to add to the new Table (report_viewers), I say:
#viewer = ReportViewer.new
#viewer.viewer_id = get_current_user[:id]
#viewer.report_id = this_report.id
#viewer.save
However, when I access the table via
ReportViewer.where(:report_id => my_report.id).last.viewer_id.name (the viewer table has a string name)
it gives me a nil class. Do you know why?
Looks like you need a join table report_viewers
report_viewers
-report_id
-viewer_id
When you detect a new viewer, add an entry with report_id and viewer_id.
Remove the record if the viewer stops viewing the report.
What i usually do is use
rails generate migration addviewertoreport
and then just add the relevant commands

EF Database First - Mapping-error 3025

New to EF and trying something out with "Database first".
Error 3025: ... :Must specify mapping for all key properties
(PurchaseUsers.PurchaseUsersId) of table PurchaseUsers.
I have in my db 3 tables:
Purchases Participants PurchaseUsers
PurchaseId ParticipantId PurchaseUsersId
... ... PurchaseId
ParticipantID
The table PurchaseUsers is to know which participant(s) is(are) using a purchase.
At first I didn't had the PK on that table but then I got the following error when trying to save a Purchase.
After googling a bit I found out that I had to add a PK to avoid this error.
Unable to update the EntitySet 'PurchaseUsers' because it has a DefiningQuery
and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
to support the current operation.
But adding the PK created the mapping-error and I just can't figure out how to solve this or create a mapping.
The table PurchaseUsers itself isn't visible in my .edmx model, but it's listed in the Store in the Model Browser.
Thanks.
UPDATE
Changed the name of a column in the database today. "Update model from database" added the new columnname to the table in the model, but did not remove the old one.
Had to start from scratch once again.
Looks like the update-function is not working very well.
This is weird. Updating the model from the database should make the model and the database to by in-sync. Try deleting and recreating the model from scratch.

how to get next ID in a table in Rails

I have a table that has following columns
id store_id store_name
id and store_id are always going to be same. Since I am new to rails...I had created it by mistake. But I can't afford to go back and change stuff now since I am using store_id at a lot of places.
Issue 1: I am making admin screen for this table. When I try to insert a record, eventhough, id gets inserted (rails automatically gets the next one) NULL is being inserted in store_id.
Is there any ways I can have same number in store_id as in id while inserting record using the create method. Is there something that can be put in the model so that it is always execute before creating a record. and there I can have access to the next Id that should be inserted?
Issue 2: Since I am making admin screens for this table now..before I was just inserting data into this table by hand. I have inserted 5 records by hand so id and store_id 1..5 already exist. Now when I try to insert a record from admin screen it says duplicate key violates constraint. could it be trying to insert id of 1 since this is the first time I am inserting record to this table using rails?
Issue 1: Until you can remove the store_id column, override the getter in the model for compatibility.
def store_id
id
end
As for Issue 2: make sure your create code is not manually setting the id. The next record should be inserted with id 6, as long as your object has no id defined. Try it from script/console.
I think you should just change your code to only use the ID.
A competent editor will be able to tell you where you have used store id in your project, and your tests will help if you introduce a regression error.
Otherwise, you could use a database trigger. Or setup a callback on the model to set store_id from the newly created id value (look at the after_create callback). Again, both of these approaches are hacks to cover up a bug in your system with an easy fix.
You can set custom primary key for ActiveRecord models:
class Store < ActiveRecord::Base
set_primary_key :store_id
end
Then you can create a new migration that removes 'id' column and updates 'store_id' column to be a primary key.

Resources