Re: [Rails] Update model without primary key
On Mon, Nov 28, 2011 at 9:30 AM, Michael Pavling <pavling@gmail.com> wrote:
> On 28 November 2011 02:27, Seb <sebastianthegreatful@gmail.com> wrote:
>> 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?
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home