Связь многие ко многим без танцев с бубном!

Josh Susser, “Many-to-many Dance-off!”, public translation into Russian from English More about this translation.

See also 12 similar translations

Translate into another language.

Participants

flipback147 points
nucleartux3 points
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next next untranslated
1 2 3 4

Many-to-many Dance-off!

Связь многие ко многим без танцев с бубном!

History of edits (Latest: flipback 1 year ago) §

I've noticed there's a bit of confusion about the differences between the two ways to create many-to-many relationships using Rails associations. That confusion is understandable, since has_many :through is still pretty new there isn't much written about it. has_and_belongs_to_many is the old, established player and most stuff out there assumes that's what you use for a many-to-many relationship. In fact, a lot of people don't seem to grasp that there is a difference at all!

Я заметил, что есть небольшая путаница между двумя различными путями создания отношений "многие ко многим" используя ассоциации в Rails. Причина путаницы в том, что метод has_many :throught еще достаточно нов и о нем мало что написано. Метод has_and_belongs_to_many старый, устоявшийся инструмент и большинство

History of edits (Latest: flipback 1 year ago) §

As we all learned from watching classic movies, the best way to tell the difference between two prospective choices is to have a dance-off. You get to see everyone's moves (which of course are an accurate reflection of inner character), nobody has to die, and the hummable tune makes it a shoe-in for an Oscar nomination. Well, it's either that or do one of those boring "compare and contrast" essays they taught us about in sixth grade English class.

So who are the players we have to choose between? Let's take a quick look at them before the music starts and we get to see their moves.

Join Table: Simple Associations

Table:

create_table "dancers_movies", :id => false do |t|

create_table "dancers_movies", :id => false do |t|

History of edits (Latest: flipback 1 year ago) §

t.column "dancer_id", :integer, :null => false

t.column "dancer_id", :integer, :null => false

History of edits (Latest: flipback 1 year ago) §

t.column "movie_id", :integer, :null => false

t.column "movie_id", :integer, :null => false

History of edits (Latest: flipback 1 year ago) §

end

end

History of edits (Latest: flipback 1 year ago) §

Models:

class Dancer < ActiveRecord::Base

class Dancer < ActiveRecord::Base

History of edits (Latest: flipback 1 year ago) §

has_and_belongs_to_many :movies

has_and_belongs_to_many :movies

History of edits (Latest: flipback 1 year ago) §

end

end

History of edits (Latest: flipback 1 year ago) §

class Movie < ActiveRecord::Base

class Movie < ActiveRecord::Base

History of edits (Latest: flipback 1 year ago) §

has_and_belongs_to_many :dancers

has_and_belongs_to_many :dancers

History of edits (Latest: flipback 1 year ago) §

end

end

History of edits (Latest: flipback 1 year ago) §

has_and_belongs_to_many associations are simple to set up. The join table has only foreign keys for the models being joined - no primary key or other attributes. (Other attributes were supported using push_with_attributes for a while, but that feature has been deprecated.) There is no model class for the join table.

Join Model: Rich Associations

Table:

create_table "appearances", do |t|

create_table "appearances", do |t|

History of edits (Latest: flipback 1 year ago) §

t.column "dancer_id", :integer, :null => false

t.column "dancer_id", :integer, :null => false

History of edits (Latest: flipback 1 year ago) §

t.column "movie_id", :integer, :null => false

t.column "movie_id", :integer, :null => false

History of edits (Latest: flipback 1 year ago) §

t.column "character_name", :string

t.column "character_name", :string

History of edits (Latest: flipback 1 year ago) §

t.column "dance_numbers", :integer

t.column "dance_numbers", :integer

History of edits (Latest: flipback 1 year ago) §

end

end

History of edits (Latest: flipback 1 year ago) §

Models:

class Appearance < ActiveRecord::Base

class Appearance < ActiveRecord::Base

History of edits (Latest: flipback 1 year ago) §
Pages: ← previous Ctrl next next untranslated
1 2 3 4