ruby on rails - ActiveRecord Join table for legacy Database -


My legacy database is that I am working with ActiveRecord to work with I went into a problem with joining tables I have the following:

  class TV show & lt; ActiveRecord :: Base set_table_name "tvshow" set_primary_key "idShow" and class episode & lt; ActiveRecord :: Base set_table_name "Episode" set_primary_key "idEpisode" end  

And then I have a table called TVSolinkPeks with 2 fields: idShow, idEpisode so I have 2 tables and connect them (Many for many relationships), although it uses non-standard foreign keys. My first thought was to create a model called TV Shops Epicink, but not the primary key. The idea was that since foreign keys are non-standard then I can use set_foreign_key and I have some control. After all I'm TeeShosho I want to say something like (Previous). Episode or episode.fund (: last) .tv_show How do I get there?

I believe that you can be a little more beautiful than the answer to Alvaro. , Though its answer is perfectly fine and there will be very similar functionality for any client of your class.

  class TvShow & lt; ActiveRecord :: Base set_table_name "tvshow" set_primary_key "idShow" has_and_belong_to_many: episode,: join_table = & gt; "TVSolink episode" ,: foreign_key => "IDShow" ,: association_foreign_key = & gt; "IDE Case" and Class Episode & lt; ActiveRecord :: Base set_table_name "Episode" set_primary_key "idEpisode" has_and_belong_s_many: tv_shows ,: join_table = & gt; "TVSolink episode" ,: foreign_key => "IdEpisode" ,: association_foreign_key = & gt; "IDSOW" and  

Note that: The option of foreign_ specifies which column is on the "this side" of the square, while: association_foreign_key specifies that column The ID is the "On the Other Side" section of the link.

In contrast with Alvaro's answer, this pattern should not mean any unnecessary objects to represent the link.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -