Ruby on Rails Monday, November 28, 2011

On Mon, Nov 28, 2011 at 8:13 AM, Norbert Melzer
<timmelzer@googlemail.com> wrote:
> Am 28.11.2011 03:27, schrieb Seb:
>

<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.

--
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