Update model without primary key

I have a telephone model in my rails application which is being updated from an android app.

The phone model consists of id, name, version, imei, etc. id is the primary key as by the rails standard.

imei is unique but not the primary key. when I update the model from the android app I dont have the id, only the imei number.

The way I see it I have 3 options.

  1. create an action called update_version, do a find_by_imei and update my model. This is actually what I do now but this isnt very restful or pretty

  2. During create assign imei to phone.id… I like this solution for it’s simplicity, but I cant get it to work (for reasons I’ll explain if requested)

  3. make two requests to the server. 1. where I do a find_by_imei and return the id 2. update normally

I really hope you guys can give me some input on how to solve this issue in a pretty way.

Thanks in advance

4. Make IMEI the primary key, but this is a quickshot and not well thought:

migration: class MakeImeiThePK < ActiveRecord::Migration   def change     # Do Stuff that alters your referencing models and tables from id     # to imei

    change_table :phones do |t|       t.remove_index :id       t.remove :id       t.add_index :imei     end   end end

model: class Phone < ActiveRecord::Base   set_primary_key :imei

  [...] end

This should provide you with a Phone.find() thats returning a Phone when feeded with IMEI. Be sure to convert related models and tables BEFORE you change the phones table or associations will be lost.

Also I am not sure if rails can handle foreign keys that are not int.

According to a StackOverflow answer I found[1], it is already something hacky to use a non int as PK...

HTH Norbert

[1] <database - Using Rails, how can I set my primary key to not be an integer-typed column? - Stack Overflow;

The phone model consists of id, name, version, imei, etc. id is the primary key as by the rails standard. imei is unique but not the primary key. when I update the model from the android app I dont have the id, only the imei number. The way I see it I have 3 options. 1. create an action called update_version, do a find_by_imei and update my model. This is actually what I do now but this isnt very restful or pretty

Why is this not pretty or RESTful? Is that because you're doing it in the same controller as your "normal" updates that have id? If so, have a separate controller for the mobile app to access by IMEI, and mixin any functionality that's shared (or subclass and overload the population of @phone depending on whether it's .find or .find_by_imei)

2. During create assign imei to phone.id... I like this solution for it's simplicity, but I cant get it to work (for reasons I'll explain if requested)

As suggested by Norbert, it would be easier to make the IMEI field the primary key.

3. make two requests to the server. 1. where I do a find_by_imei and return the id 2. update normally

This might be easiest... but I'd be keen to see the controller code you're using at the moment, because I assume you're doing something like this:    @phone = Phone.find(params[:id])

and you *might* be able to change that to :    @phone = Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])

(or DRY it up to a method:    @phone = find_phone_from_params

  private   def find_phone_from_params     Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])   end )

<snip />

4. Make IMEI the primary key, but this is a quickshot and not well thought:

migration: class MakeImeiThePK < ActiveRecord::Migration def change # Do Stuff that alters your referencing models and tables from id # to imei

change_table :phones do |t| t.remove_index :id t.remove :id t.add_index :imei end end end

model: class Phone < ActiveRecord::Base set_primary_key :imei

[...] end

This should provide you with a Phone.find() thats returning a Phone when feeded with IMEI.

There is no disadvantage to this? Is this a rails 3 feature?

Be sure to convert related models and tables BEFORE you change the phones table or associations will be lost.

This is related to existing data? Because otherwise I'm not sure what you mean.

Also I am not sure if rails can handle foreign keys that are not int.

According to a StackOverflow answer I found[1], it is already something hacky to use a non int as PK...

IMEI are ints. Atleast the ones I've encountered so far.

The phone model consists of id, name, version, imei, etc. id is the primary key as by the rails standard. imei is unique but not the primary key. when I update the model from the android app I dont have the id, only the imei number. The way I see it I have 3 options. 1. create an action called update_version, do a find_by_imei and update my model. This is actually what I do now but this isnt very restful or pretty

Why is this not pretty or RESTful? Is that because you're doing it in the same controller as your "normal" updates that have id?

Yes, and just because I did it in a odd way... I'm new to rails.

If so, have a separate controller for the mobile app to access by IMEI, and mixin any functionality that's shared (or subclass and overload the population of @phone depending on whether it's .find or .find_by_imei)

2. During create assign imei to phone.id... I like this solution for it's simplicity, but I cant get it to work (for reasons I'll explain if requested)

As suggested by Norbert, it would be easier to make the IMEI field the primary key.

Yeah, and right now I'm leaning towards that way.

3. make two requests to the server. 1. where I do a find_by_imei and return the id 2. update normally

This might be easiest... but I'd be keen to see the controller code you're using at the moment, because I assume you're doing something like this: @phone = Phone.find(params[:id])

and you *might* be able to change that to : @phone = Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])

(or DRY it up to a method: @phone = find_phone_from_params

private def find_phone_from_params Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei]) end )

The problem with this is that the update action is invoked by posting to /phones/id. And I dont have that id when posting. If i simply post to /phones the create method will be invoked, right?

The phone model consists of id, name, version, imei, etc. id is the primary key as by the rails standard. imei is unique but not the primary key. when I update the model from the android app I dont have the id, only the imei number. The way I see it I have 3 options. 1. create an action called update_version, do a find_by_imei and update my model. This is actually what I do now but this isnt very restful or pretty

Why is this not pretty or RESTful? Is that because you're doing it in the same controller as your "normal" updates that have id?

Yes, and just because I did it in a odd way... I'm new to rails.

If so, have a separate controller for the mobile app to access by IMEI, and mixin any functionality that's shared (or subclass and overload the population of @phone depending on whether it's .find or .find_by_imei)

2. During create assign imei to phone.id... I like this solution for it's simplicity, but I cant get it to work (for reasons I'll explain if requested)

As suggested by Norbert, it would be easier to make the IMEI field the primary key.

Yeah, and right now I'm leaning towards that way.

3. make two requests to the server. 1. where I do a find_by_imei and return the id 2. update normally

This might be easiest... but I'd be keen to see the controller code you're using at the moment, because I assume you're doing something like this:   @phone = Phone.find(params[:id])

and you *might* be able to change that to :   @phone = Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])

(or DRY it up to a method:   @phone = find_phone_from_params

private def find_phone_from_params    Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei]) end )

The problem with this is that the update action is invoked by posting to /phones/id. And I dont have that id when posting. If i simply post to /phones the create method will be invoked, right?

When you're posting from the phone, you DO have the IMEI, right? Your phone-specific controller can know this, and use the following pattern:

  def update     @phone = Phone.find_by_imei(params[:id])     @phone.update_attributes(params[:phone])     ...   end

Just substitute the imei for the id in your form_for tag, and Bob's your uncle.

Walter

This should provide you with a Phone.find() thats returning a Phone when feeded with IMEI.

There is no disadvantage to this? Is this a rails 3 feature?

I dont know about disadvantages, but I never tested... Also I dont know if this is rails 3 or could work with earlier versions. Its just a hack that I found on stackoverflow by googling around. I never tried this.

Be sure to convert related models and tables BEFORE you change the phones table or associations will be lost.

This is related to existing data? Because otherwise I'm not sure what you mean.

Dunno... To say that I have to know your existing database structure, but how should I?

If you have any models that are belonging to a phone, then you have to change their foreign key (phone_id) to the IMEI instead of the ID. Also you have to alter the type of that column if needed, could be much of table altering.

Also I am not sure if rails can handle foreign keys that are not int.

According to a StackOverflow answer I found[1], it is already something hacky to use a non int as PK...

IMEI are ints. Atleast the ones I've encountered so far.

I had IMEIs in the head with dashes, also my phone reports it with dashes. But according to wikipedia this dashes are only for grouping and readability, they are not used when transferring them, or at least can be stripped if your clients transmits them.

But as I am writing, another possibility came into my mind.

But I dont know how that works with belonging models...

Just overide the find-method in your model, like this:

def self.find(imei)   self.find_by_imei(imei) end

eventually you need to tweak a little bit... Its a thought...

In any case, I would create another branch and test there in a sondbox if you are using a VCS, in any other case I would try it in another directory.

When all your existing tests are passing after changes then you could do manually testing, and if that also shows no breakages, then you can deploy and merge.

But hold back a backup, could be better. And I am talking about a database backup also!

HTH Norbert

The phone model consists of id, name, version, imei, etc. id is the primary key as by the rails standard. imei is unique but not the primary key. when I update the model from the android app I dont have the id, only the imei number. The way I see it I have 3 options. 1. create an action called update_version, do a find_by_imei and update my model. This is actually what I do now but this isnt very restful or pretty

Why is this not pretty or RESTful? Is that because you're doing it in the same controller as your "normal" updates that have id?

Yes, and just because I did it in a odd way... I'm new to rails.

If so, have a separate controller for the mobile app to access by IMEI, and mixin any functionality that's shared (or subclass and overload the population of @phone depending on whether it's .find or .find_by_imei)

2. During create assign imei to phone.id... I like this solution for it's simplicity, but I cant get it to work (for reasons I'll explain if requested)

As suggested by Norbert, it would be easier to make the IMEI field the primary key.

Yeah, and right now I'm leaning towards that way.

3. make two requests to the server. 1. where I do a find_by_imei and return the id 2. update normally

This might be easiest... but I'd be keen to see the controller code you're using at the moment, because I assume you're doing something like this: @phone = Phone.find(params[:id])

and you *might* be able to change that to : @phone = Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])

(or DRY it up to a method: @phone = find_phone_from_params

private def find_phone_from_params Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei]) end )

The problem with this is that the update action is invoked by posting to /phones/id. And I dont have that id when posting. If i simply post to /phones the create method will be invoked, right?

When you're posting from the phone, you DO have the IMEI, right? Your phone-specific controller can know this, and use the following pattern:

I sure do.

def update @phone = Phone.find_by_imei(params[:id]) @phone.update_attributes(params[:phone]) ... end

Just substitute the imei for the id in your form_for tag, and Bob's your uncle.

How would I go about that?

This should provide you with a Phone.find() thats returning a Phone when feeded with IMEI.

There is no disadvantage to this? Is this a rails 3 feature?

I dont know about disadvantages, but I never tested... Also I dont know if this is rails 3 or could work with earlier versions. Its just a hack that I found on stackoverflow by googling around. I never tried this.

Be sure to convert related models and tables BEFORE you change the phones table or associations will be lost.

This is related to existing data? Because otherwise I'm not sure what you mean.

Dunno... To say that I have to know your existing database structure, but how should I?

If you have any models that are belonging to a phone, then you have to change their foreign key (phone_id) to the IMEI instead of the ID. Also you have to alter the type of that column if needed, could be much of table altering.

Also I am not sure if rails can handle foreign keys that are not int.

According to a StackOverflow answer I found[1], it is already something hacky to use a non int as PK...

IMEI are ints. Atleast the ones I've encountered so far.

I had IMEIs in the head with dashes, also my phone reports it with dashes. But according to wikipedia this dashes are only for grouping and readability, they are not used when transferring them, or at least can be stripped if your clients transmits them.

If dashes occours I'll strip them.

But as I am writing, another possibility came into my mind.

But I dont know how that works with belonging models...

Just overide the find-method in your model, like this:

def self.find(imei) self.find_by_imei(imei) end

eventually you need to tweak a little bit... Its a thought...

I cant comprehend the side effects of this. And The first solution of changing the pk to imei is turning into a nightmare... the imei nil and misc other stuff.

Is there no way I can simply assign imei to phone.id during create? That would make my life easier.

Can't I just create a after_create filter and assign imei to phone.id ?

4) Drop the imei column altogether and use that value as the id.

Obviously that means fixing your foreign key refs, etc., so it's some work, but it simplifies in the long run.

I have an existing 2.3.x app with a non-system-generated varchar id column (due to a legacy DB schema) and it works just fine.

HTH,

Only when wiping the database and creating the phone-table withoud id, then creating the row id by hand as type integer.

Following would probably work:

0) DATABASE BACKUP!!! 1) Create a new temporary table, exactly as the phone-table is now. 2) Copy every single phone into that new table 3) drop old phone table 4) create a new table for your phones, make sure you create it with the    :id => false option, then t.integer :id in the migration itself.    Probably you will need another bigger version of int! 5) Update references! (You can find out the id <=> imei still in    temporary table) 6) Drop temporary table 7) create an index over your id-column

This could work all in one migration... NO you have to do this in one migration just to be sure no one does anything in that time. If you do that on the liveserver later, I strongly encourage you to stop the service until the migration is run.

Be sure to test it locally in every single workcase! Even that usecases that should not be possible!

You cant revert the changes easily!

HTH Norbert

<snip />

This isn't a live system. I'm developing as we speak.

Right now I'm doing the following in my controller.

73 def create 74 @device = Device.new(params[:device]) 75 @device.id = @device.device_id.gsub('-', '').to_i ...

(I dont care that I have two columns which contains the same data. I've spend way too long time on different solutions already.)

However this fails to work in my update method.

91 def update 92 @device = Device.find(params[:id]) 93 @device.id = params[:device][:device_id].gsub('-', '').to_i ...

The resulting error message is: Couldn't find Device with id=777666

I've tried to @device.save after assigning to id with the same result.

Personally I advise against going against the convention of id being the primary key with its value determined by Rails. It is possible but it is hard work, and it is liable to be the sort of thing that continues to give grief over the years as Rails moves forward for example, and the method used to get round it no longer works. In your situation I would just include the imei as a separate field, pass that as a parameter in the form and use it to look up the record.

The only situation where I would go against the convention is when I have a legacy database whose schema I cannot modify. And I mean *really* cannot modify.

Colin

No, but if you post to /phones/random_string and the imei is in the params, the code I posted above should work... It certain smells - and if it were my code, I'd really be looking at a separate controller to handle the imei-based queries, but it may get you over the hump.

I couldnt agree more. I have dropped the idea about using a secondary column as PK.

I actually started using phone.find(...) || phone.find_by_imei(...) and is now posting to /phones/not_to_be_found becasue I was afraid of accidential overlapping id's.

Thanks for the support.

best regards, Seb

ps. also thanks to anyone else who participated in this thread.