Discussion:
Update model without primary key
Seb
2011-11-28 02:27:47 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4bRGwagbpQQJ.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Norbert Melzer
2011-11-28 07:13:59 UTC
Permalink
Post by Seb
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
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]
<http://stackoverflow.com/questions/1200568/using-rails-how-can-i-set-my-primary-key-to-not-be-an-integer-typed-column>
Sebastian
2011-11-28 14:57:14 UTC
Permalink
On Mon, Nov 28, 2011 at 8:13 AM, Norbert Melzer
<snip />
Post by Norbert Melzer
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
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?
Post by Norbert Melzer
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.
Post by Norbert Melzer
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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Norbert Melzer
2011-11-28 16:27:31 UTC
Permalink
Post by Sebastian
On Mon, Nov 28, 2011 at 8:13 AM, Norbert Melzer
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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
Sebastian
2011-11-28 16:40:23 UTC
Permalink
On Mon, Nov 28, 2011 at 5:27 PM, Norbert Melzer
Post by Norbert Melzer
Post by Sebastian
On Mon, Nov 28, 2011 at 8:13 AM, Norbert Melzer
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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.
Post by Norbert Melzer
But as I am writing, another possibility came into my mind.
But I dont know how that works with belonging models...
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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sebastian
2011-11-28 16:58:47 UTC
Permalink
On Mon, Nov 28, 2011 at 5:40 PM, Sebastian
Post by Sebastian
On Mon, Nov 28, 2011 at 5:27 PM, Norbert Melzer
Post by Norbert Melzer
Post by Sebastian
On Mon, Nov 28, 2011 at 8:13 AM, Norbert Melzer
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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.
Post by Sebastian
Post by Norbert Melzer
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.
Post by Norbert Melzer
But as I am writing, another possibility came into my mind.
But I dont know how that works with belonging models...
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 ?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Norbert Melzer
2011-11-28 17:00:40 UTC
Permalink
Post by Sebastian
Is there no way I can simply assign imei to phone.id during create?
That would make my life easier.
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
Sebastian
2011-11-28 17:42:23 UTC
Permalink
On Mon, Nov 28, 2011 at 6:00 PM, Norbert Melzer
Post by Norbert Melzer
Post by Sebastian
Is there no way I can simply assign imei to phone.id during create?
That would make my life easier.
Only when wiping the database and creating the phone-table withoud id,
then creating the row id by hand as type integer.
<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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-11-28 19:54:23 UTC
Permalink
Post by Sebastian
On Mon, Nov 28, 2011 at 6:00 PM, Norbert Melzer
Post by Norbert Melzer
Post by Sebastian
Is there no way I can simply assign imei to phone.id during create?
That would make my life easier.
Only when wiping the database and creating the phone-table withoud id,
then creating the row id by hand as type integer.
<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
...
(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
...
Couldn't find Device with id=777666
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sebastian
2011-11-28 20:26:03 UTC
Permalink
Post by Colin Law
Post by Sebastian
On Mon, Nov 28, 2011 at 6:00 PM, Norbert Melzer
Post by Norbert Melzer
Post by Sebastian
Is there no way I can simply assign imei to phone.id during create?
That would make my life easier.
Only when wiping the database and creating the phone-table withoud id,
then creating the row id by hand as type integer.
<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
...
(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
...
Couldn't find Device with id=777666
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.
I couldnt agree more. I have dropped the idea about using a secondary
column as PK.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-11-28 08:30:43 UTC
Permalink
Post by Seb
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)
Post by Seb
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.
Post by Seb
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
)
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sebastian
2011-11-28 15:07:55 UTC
Permalink
Post by Michael Pavling
Post by Seb
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.
Post by Michael Pavling
If so, have a separate controller for the mobile app to access by
IMEI, and mixin any functionality that's shared (or subclass and
.find_by_imei)
Post by Seb
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.
Post by Michael Pavling
Post by Seb
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
 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?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-11-28 16:19:56 UTC
Permalink
Post by Sebastian
Post by Michael Pavling
Post by Seb
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.
Post by Michael Pavling
If so, have a separate controller for the mobile app to access by
IMEI, and mixin any functionality that's shared (or subclass and
.find_by_imei)
Post by Seb
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.
Post by Michael Pavling
Post by Seb
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
@phone = Phone.find(params[:id])
@phone = Phone.find_by_id(params[:id]) || Phone.find_by_imei(params[:imei])
@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
Post by Sebastian
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sebastian
2011-11-28 16:32:40 UTC
Permalink
Post by Sebastian
Post by Michael Pavling
Post by Seb
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.
Post by Michael Pavling
If so, have a separate controller for the mobile app to access by
IMEI, and mixin any functionality that's shared (or subclass and
.find_by_imei)
Post by Seb
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.
Post by Michael Pavling
Post by Seb
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
 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?
I sure do.
 def update
   ...
 end
Just substitute the imei for the id in your form_for tag, and Bob's your uncle.
How would I go about that?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-11-28 20:01:56 UTC
Permalink
Post by Sebastian
Post by Michael Pavling
 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.
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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sebastian
2011-11-28 20:30:39 UTC
Permalink
Post by Michael Pavling
Post by Sebastian
Post by Michael Pavling
 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.
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 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.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-11-28 16:59:23 UTC
Permalink
Post by Seb
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.
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,
--
Hassan Schroeder ------------------------ hassan.schroeder-***@public.gmane.org
http://about.me/hassanschroeder
twitter: @hassan
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Loading...