Ruby on Rails Monday, November 28, 2011

On Mon, Nov 28, 2011 at 9:01 PM, Michael Pavling <pavling@gmail.com> wrote:
> On 28 November 2011 15:07, Sebastian <sebastianthegreatful@gmail.com> wrote:
>>> 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.
>
> 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@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.

No comments:

Post a Comment