Modifying Database Ids from Rails Console

Modifying Database IDs from Rails Console?

Would I possibly have to do this through direct SQL in sqlite?

Yes.

The whole point of ActiveRecord is that is abstracts DB functions and just returns collections of data. You shouldn't be worrying about the ID of a record, that is something specific to the DB. Off the top of my head I can't think of any reasons to reference the model's ID.

If your app depends on having a sequenced number then you should add another field to the model which has this. For instance, if I have a store with products (a Product model) and I give the ID number the DB provides to other vendors. Two weeks later, my boss asks me to have a unique, but similar ID for two variations of products: "45a" and "45b". Nuts. The ID field should only be used by to the database and ActiveRecord, not you or your users, to identify the record.

There is a small chance that there might be an obscure method which force sets the ID if the DB allows it, but it is obscure for a reason. Don't try and find it :)

All that being said, type ruby script/dbconsole to quickly pull up the sqlite interface without having to type your password.

Also, if you delete the sqlite database that will reset the counter and start at 0. With great power comes great responsibility.

EDIT

If I remember correctly Dave Thomas wrote about this somewhere. Perhaps here?

Edit a database record through the Rails console

Step 1. Find the record.

Step 2. Edit the record.

Assuming title is unique, the following should work:

> m = Meme.where(:title => 'Balloon frihgtens cat').first
> m.title = 'Balloon frightens cat'
> m.save

Read up http://guides.rubyonrails.org/active_record_querying.html to learn more about using active record.

How to change primary ID of a record in Rails?

I'm not sure as to why it prevents that action, but it does for sure prevent it.

You can bypass this using the update_all method on for the user.

User.where(id: 7).update_all(id: 9)

Though if possible, you really should avoid doing this.

In Rails Console. how to update a field for all records?

You're looking for update_all. See doc.

Beware, no callbacks are triggered this way.

Unable to update database column value from rails console

Ok I figured it out, it's something related to object_id

You will have to do

x=r.reason.dup

Then it works fine.

source: It's because the way ActiveRecord detects that attributes are changed is through the setter. Therefore, if you use gsub! on an attribute, ActiveRecord doesn't know it needs to update the database.

Edit: It has nothing related to DB type.

Rails: force specific id for new record (or change id of existing)

Your update statement is constructed using the id of the in memory object, which you have set to 2. You can't update a record that doesn't exist.

If you want to stay in active record land I think you can do: User.update(5, id: 2)

Failing that, you can definitely do it in SQL. UPDATE users SET id = 2 WHERE id = 5.

Overriding id on create in ActiveRecord

id is just attr_protected, which is why you can't use mass-assignment to set it. However, when setting it manually, it just works:

o = SomeObject.new
o.id = 8888
o.save!
o.reload.id # => 8888

I'm not sure what the original motivation was, but I do this when converting ActiveHash models to ActiveRecord. ActiveHash allows you to use the same belongs_to semantics in ActiveRecord, but instead of having a migration and creating a table, and incurring the overhead of the database on every call, you just store your data in yml files. The foreign keys in the database reference the in-memory ids in the yml.

ActiveHash is great for picklists and small tables that change infrequently and only change by developers. So when going from ActiveHash to ActiveRecord, it's easiest to just keep all of the foreign key references the same.

Rails Console find users by array of ids

For an array, you can use one of these:

# Will raise exception if any value not found
User.find( [1,3,5] )

# Will not raise an exception
User.find_all_by_id( [1,3,5] ) # Rails 3
User.where(id: [1,3,5]) # Rails 4

If you happen to be using a range, you can use these:

# Will raise exception if any value not found
User.find((1..4).to_a) #same as User.find([1,2,3,4])

# Will not raise an exception
User.find_all_by_id(1..4) # Rails 3
User.where(id: 1..4) # Rails 4

As @diego.greyrobot notes in a comment, a range causes a SQL BETWEEN clause, whereas an array causes a SQL IN clause.

Don't use User.find_by_id() -- It will only return one record, no matter how may IDs you pass in.

Rails console: Update attributes and save to DB

Strange fix:

By typing it in 2 steps that works:

1) Order.all.each do |tname| tname.team = User.where(:id => tname.user_id).pluck(:team).shift

2) tname.save end

Instead of

Order.all.each do |tname| tname.team = User.where(:id => tname.user_id).pluck(:team).shift tname.save end.

Absolutely no clue why..



Related Topics



Leave a reply



Submit