Sei sulla pagina 1di 4

ActiveRecord Join table for legacy Database

I have a legacy database that I'm working on getting ActiveRecord to work with. I've run into a problem with join tables. I have the following:
class TvShow < ActiveRecord::Base set_table_name "tvshow" set_primary_key "idShow" end class Episode < ActiveRecord::Base set_table_name "episode" set_primary_key "idEpisode" end

And then I have a table called tvshowlinkepisode that has 2 fields: id how! id"pisode o I have 2 tables and a join between them #so a many to many relationship$! however the join uses non%standard foreign keys. &y first thought was to create a model called 'v how"pisode(ink but there isn't a primary key. 'he idea was that since the foreign keys are non%standard I could use the set)foreign)key and have some control. *ltimately I want to say something like 'v how.find#:last$.episodes or "pisode.find#:last$.tv)show. +ow do I get there,

4 Answers
I believe you can be slightly more elegant than Alvaro's answer using options to has)and)belongs)to)many! though his answer is perfectly fine and will result in fairly identical functionality for any clients of your class.
class TvShow < ActiveRecord::Base set_table_name "tvshow" set_primary_key "idShow" has_and_belong_to_many :episodes :!oin_table "# "tvshowlinkepisode" :$oreign_key "# "idShow" :association_$oreign_key "# "idEpisode" end class Episode < ActiveRecord::Base set_table_name "episode" set_primary_key "idEpisode" has_and_belongs_to_many :tv_shows :!oin_table "# "tvshowlinkepisode" :$oreign_key "# "idEpisode" :association_$oreign_key "# "idShow" end

-ote that the :foreign)key option specifies which column is the id for the class on .this side. of the link! while :association)foreign)key specifies the column that is the id for the class on the .other side. of the link. /ontrast this with Alvaro's answer! this pattern should avoid instantiation of any unnecessary objects to represent the link. answered 1ul 22 '34 at 22:56 share0improve this answer animal 5!7785956 I would think an episode can only belong to one particular tv show! not to many. : t;wikinger 1ul 27 '34 at 6:39 I would think that too! <t;wikinger! but I guess the e;ample is that if a show has a spin% off series that at sometime during both of their respective series they may have an episode where the two come together. At any rate! it's interesting getting someone else's database to make logical sense through activerecord. : ni;terrimus 1ul 27 '34 at 6:62 add comment 'his work for you...

class TvShow < ActiveRecord::Base set_table_name "tvshow" set_primary_key "idShow" has_many :tv_show_link_episode :$oreign_key "# %idShow% has_many :episodes :thro&gh "# :tv_show_link_episode end class Episode < ActiveRecord::Base set_table_name "episode" set_primary_key "idEpisode" has_many :tv_show_link_episode :$oreign_key "# %idEpisode% has_many :tv_shows :thro&gh "# :tv_show_link_episode end class TvShow'inkEpisode < ActiveRecord::Base set_table_name "tvshowlinkepisode" ( the $oreign key is named by the ( the primary key name is $or the belongs_to :tv_show :$oreign_key belongs_to :episode :$oreign_key end TvShow'inkEpisode $ield primary key o$ the associated class "# %idShow% "# %idEpisode%

answered 1ul 22 '34 at 25:34 share0improve this answer Alvaro 'alavera 57=6 >5! this looks fine to me : marcgg 1ul 22 '34 at 25:59 add comment 'he relationship is one%to%many so you need to use the belongs)to?has))many relationship in the tables. If your database has views! you could mask the non%standard foreign keys by a view for the tables. -ot sure if that fits 533@ what you need! but I hope it at least gives you an idea. answered 1ul 22 '34 at 23:65 share0improve this answer edited 1ul 22 '34 at 23:69

t;wikinger 5!835429 Interesting idea. I haven't worked with database views before. I don't have the ability to change anything about the database! so I'm not sure if database views would work here. An the subject of the has)and)belongs)to)many relationship! doesn't that fall apart if I have non%standard primary keys, I thought that by using a third model I could do a has)and)belongs)to)many)through relationship. : ni;terrimus 1ul 22 '34 at 23:6=

"r make that non%standard foreign in key in the above response: An the subject of the has)and)belongs)to)many relationship! doesn't that fall apart if I have non%standard foreign keys, : ni;terrimus 1ul 22 '34 at 23:68 <ni;terrimus! right! thanks : t;wikinger 1ul 22 '34 at 23:69 Bell! the idea is that you use the standard foreign key! the view translates basically your standard foreign key to the non%standard one in the table. A view can be as simple as re%naming the fields #i.e. /R"A'" CI"B 'v howCiew A "("/' tvshow! id how A how)id DRA& 'v how$. As long as the view has a one%to%one relationship to the table you can also use it for inserts and updates. : t;wikinger 1ul 22 '34 at 23:26 add comment Bith this! you don't need to set table views! actually! tables views it's not .'he Rails Bay.! 'ry this:
## TvShow)$ind*+,)episodes ("# ret&rns an array with -(<Episode idEpisode: + +"#. ## Episode)$ind*+,) tv_shows ("# ret&rns an array with -(<TvShow idShow: + test: "Episode

up vote 3 down 'hen you can do some stuff like: vote

test: "tvshow +"#.

e " Episode)$ind*+, TvShow)$ind*+,) episodes << e ("# this make the proper association

answered 1ul 22 '34 at 25:65 share0improve this answer Alvaro 'alavera 57=6 add comment