Imagine you have an application that has URLs like the following for say books: http://url/books/show/23, where 23 is the database id of a book. But, using ISBNs for book urls might be better, so you''d have http://url/books/show/097669400X, where 097669400X is the ISBN number for a book. What would be the best way to integrate that into an existing site with as little pain as possible? Would I have to go through and change all the links to the books? Routes?
Joe Van Dyk wrote:> Imagine you have an application that has URLs like the following for > say books: http://url/books/show/23, where 23 is the database id of a > book. But, using ISBNs for book urls might be better, so you''d have > http://url/books/show/097669400X, where 097669400X is the ISBN number > for a book. > > What would be the best way to integrate that into an existing site > with as little pain as possible? Would I have to go through and > change all the links to the books? Routes?Definitely a custom route. You want to place it above the route that handles the normal ID style. The key thing is coming up with a regex pattern than matches on the ISBN number, but doesn''t match on a standard ID. I would also send it to a custom named controller, rather than handling it in the standard "show", but that might just be me. :) As for changing the links to all your books, that''s going to be up to you. What you might be able to do is use a named route and push your book URLs through it. -Brian
Joe Van Dyk wrote:> Imagine you have an application that has URLs like the following for > say books: http://url/books/show/23, where 23 is the database id of a > book. But, using ISBNs for book urls might be better, so you''d have > http://url/books/show/097669400X, where 097669400X is the ISBN number > for a book. > > What would be the best way to integrate that into an existing site > with as little pain as possible? Would I have to go through and > change all the links to the books? Routes?You want to keep both methods alive I take it? Based on the size of an ISBN and the data type of your id field, can you safely say that an ISBN will always be able to identified given an ISBN vs. an id? If yes, you could just modify the action to determine first what to query... Zach
You don''t have to use ISBN as id in order to do that. Just use Book.find_by_isbn(params[:id]) in your controller (or change your routes.rb and name the parameter :isbn). Regards, Tomas Jogin On 8/26/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Imagine you have an application that has URLs like the following for > say books: http://url/books/show/23, where 23 is the database id of a > book. But, using ISBNs for book urls might be better, so you''d have > http://url/books/show/097669400X, where 097669400X is the ISBN number > for a book. > > What would be the best way to integrate that into an existing site > with as little pain as possible? Would I have to go through and > change all the links to the books? Routes? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Joe Van Dyk wrote:> Imagine you have an application that has URLs like the following for > say books: http://url/books/show/23, where 23 is the database id of a > book. But, using ISBNs for book urls might be better, so you''d have > http://url/books/show/097669400X, where 097669400X is the ISBN number > for a book. > > What would be the best way to integrate that into an existing site > with as little pain as possible? Would I have to go through and > change all the links to the books? Routes?Here is the way i do it: Define your isbn in your book table and make an index on it add a route map.connect ''books/show/:isbn'', :controller => ''book'', :action => ''show'' and in BookCOntroller: def show if params[:isbn] @person = Book.find_by_isbn(params[:isbn]) else @person = Book.find(params[:id]) end end That''s all! -- Jean-Christophe Michel
ISBNs can be identified by length, content, and checksum. They will never overlap with the numeric record IDs. So if you want to support both with minimal hassle, just throw a before_filter around your controller to recognize when you have an ISBN for an ID, vs. a record ID, and fix up the params accordingly. Wherever you generate an link you can just through ", :id => @book.isbn" (or equivalent) on it if you''d prefer the user to see that form. If you want to use ISBN as the primary form, I don''t see why you couldn''t modify your table to use the ISBN as the id (test it on a copy first of course). Be warned though that some publishers (e.g. Wallmart Press) misuse the system and issue multiple books with the same ISBN, and older books did not have them (SBNs and EANs can be mapped, but even earlier books had none at all). --MarkusQ
In article <c715e64050826122390ac42f-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>, joevandyk- Re5JQEeQqe8AvxtiuMwx3w-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says...> But, using ISBNs for book urls might be better, so you''d have > http://url/books/show/097669400X, where 097669400X is the ISBN number > for a book. > > What would be the best way to integrate that into an existing site > with as little pain as possible?OT: While you''re at it, make sure you''re ready for 13- and 14-digit EANs which are replacing ISBNs over the next few years... -- Jay Levitt | Wellesley, MA | I feel calm. I feel ready. I can only Faster: jay at jay dot fm | conclude that''s because I don''t have a http://www.jay.fm | full grasp of the situation. - Mark Adler
Jean-Christophe Michel wrote:> Joe Van Dyk wrote: > >>Imagine you have an application that has URLs like the following for >>say books: http://url/books/show/23, where 23 is the database id of a >>book. But, using ISBNs for book urls might be better, so you''d have >>http://url/books/show/097669400X, where 097669400X is the ISBN number >>for a book. > > Here is the way i do it: > > Define your isbn in your book table and make an index on it > > add a route > map.connect ''books/show/:isbn'', :controller => ''book'', :action => ''show''Actually, he will need to specify a pattern for the :isbn part, so that it can be distiguished from :id. And, this route must be placed above the generic route so that it has a chance to be called. But yes, this is basically all there is to it. Although, I would probably name the route "map.isbn" so that it can be used in link generation calls, when/if he want''s to build links to books, based off their isbn value. -Brian
On 8/27/05, Jay Levitt <jay-news-WxwZQdyI2t0@public.gmane.org> wrote:> In article <c715e64050826122390ac42f-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>, joevandyk- > Re5JQEeQqe8AvxtiuMwx3w-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says... > > But, using ISBNs for book urls might be better, so you''d have > > http://url/books/show/097669400X, where 097669400X is the ISBN number > > for a book. > > > > What would be the best way to integrate that into an existing site > > with as little pain as possible? > > OT: While you''re at it, make sure you''re ready for 13- and 14-digit EANs > which are replacing ISBNs over the next few years...Thanks for all the ISBN-specific information, but my app isn''t using them. :-)
On 8/27/05, Brian V. Hughes <brianvh-ilmOVS5JQ6Xj7r8U7pfrKh2eb7JE58TQ@public.gmane.org> wrote:> Jean-Christophe Michel wrote: > > Joe Van Dyk wrote: > > > >>Imagine you have an application that has URLs like the following for > >>say books: http://url/books/show/23, where 23 is the database id of a > >>book. But, using ISBNs for book urls might be better, so you''d have > >>http://url/books/show/097669400X, where 097669400X is the ISBN number > >>for a book. > > > > Here is the way i do it: > > > > Define your isbn in your book table and make an index on it > > > > add a route > > map.connect ''books/show/:isbn'', :controller => ''book'', :action => ''show'' > > Actually, he will need to specify a pattern for the :isbn part, so that > it can be distiguished from :id. And, this route must be placed above > the generic route so that it has a chance to be called. But yes, this is > basically all there is to it. Although, I would probably name the route > "map.isbn" so that it can be used in link generation calls, when/if he > want''s to build links to books, based off their isbn value.Would it be better to have something like map.isbn ''books/isbn/:isbn'', :controller => ''book'', :action => ''isbn'' And then have that action be something like this? def isbn if @params[:isbn] @book = Books.find_by_isbn @params[:isbn] render :action => ''show'' end end Like that? How should you detect errors, like ISBN not being passed a :isbn or if there''s a bad ISBN number?
On 27.8.2005, at 18.35, Markus wrote:> > If you want to use ISBN as the primary form, I don''t see why you > couldn''t modify your table to use the ISBN as the id (test it on a > copy > first of course). Be warned though that some publishers (e.g. > Wallmart > Press) misuse the system and issue multiple books with the same ISBN, > and older books did not have them (SBNs and EANs can be mapped, but > even > earlier books had none at all).There''s enough reasons not to use ISBN''s as the primary key of any table ;-) They''re just not unique and as Jay suggested, they''re about to change in the near future. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine wrote:> There''s enough reasons not to use ISBN''s as the primary key of any > table ;-) > > They''re just not unique and as Jay suggested, they''re about to change > in the near future.In fact, in the Rails book itself in section 14.3 "Primary Keys and IDs" it even has this as the specific example of why not to do this: You may have noticed that our sample database tables all define an integer column called id as their primary key. This is an Active Record convention. "But wait!" you cry. "Shouldn''t the primary key of my orders table be the order number or some other meaningful column? Why use an artificial primary key such as id?" The reason is largely a practical one - the format of external data may change over time. For example, you might think that the ISBN of a book would make a good primary key in a table of books. After all, ISBNs are unique. But as this particular book is being written, the publishing industry in the US is gearing up for a major change as additional digits are added to all ISBNs. If we''d used the ISBN as the primary key in a table of books, we''d have to go through and update each row to reflect this change. But then we''d have another problem. There''ll be other tables in the database that reference rows in the books table via the primary key. We can''t change the key in the books table unless we first go through and update all of these references. And that will involve dropping foreign key constraints, updating tables, updating the books table, and finally reestablishing the constraints. All in all, something of a pain. If we use our own internal value as a primary key, things work out a lot better. No third party can come along and arbitrarily tell us to change things - we control our own keyspace. And if something such as the ISBN does need to change, it can change without affecting any of the existing relationships in the database. In effect, we''ve decoupled the knitting together of rows from the external representation of data in those rows. Seems like good enough reasoning to me! :-) ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/