…se volessi fare un collegamento molti a molti tra due tabelle?
Dovrei creare una terza tabella con le chiavi primarie delle due
tabelle.
Tali due chiavi vengono a formare una chiave primaria composita per la
tabella di collegamento…si ma…come specifico, usando le
migration
che si tratta di una chiave composita?
Boh…
2009/1/7 Mauro [email protected]
…se volessi fare un collegamento molti a molti tra due tabelle?
Dovrei creare una terza tabella con le chiavi primarie delle due tabelle.
Tali due chiavi vengono a formare una chiave primaria composita per la
tabella di collegamento…si ma…come specifico, usando le migration
che si tratta di una chiave composita?
Boh…
Niente?
2009/1/7 Mauro [email protected]:
…se volessi fare un collegamento molti a molti tra due tabelle?
Dovrei creare una terza tabella con le chiavi primarie delle due tabelle.
Hai due possibilità. Puoi usare una join table oppure un join model
tramite l’opzione :through (particolarmente, se ti servono altri
campi).
Tali due chiavi vengono a formare una chiave primaria composita per la
tabella di collegamento…si ma…come specifico, usando le migration
che si tratta di una chiave composita?
Boh…
Se scegli di proseguire con la join table, questa tabella non ha
modello corrispondente e non ha bisogno di una chiave primaria id.
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
end
In your migration file
create_table :categories_posts, :id => false do |t|
t.integer :category_id
t.integer :post_id
end
Ciao,
Antonio
http://antoniocangiano.com - Zen and the Art of Programming
http://math-blog.com - Mathematics is wonderful!
http://stacktrace.it - Aperiodico di resistenza informatica
Currently writing “Ruby on Rails for Microsoft Developers” for Wrox.
2009/1/25 Mauro [email protected]:
Se uso has_many :through devo creare la tabella di collegamento mentre se
uso has_and_belong_to_many la terza tabella non la dovrei creare?
la terza tabella devi crearla comunque,
però:nel caso has_and_belongs_to_many:
in migration
def self.up
create_table ‘categories’ do |t|
t.string :name
t.string :qualcosa
t.timestamps
end
create_table ‘products’ do |t|
t.string :name
t.string :description
t.timestamps
end
create_table ‘categories_products’, :id => false do |t|
t.column :category_id, :integer
t.column :product_id, :integer
end
end
models/category.rb
class Category < ActiveRecord::Base
has_and_belongs_to_many: :produtcs
end
models/product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many: :categories
end
non devi creare un modello per categories_products.
così puoi fare product.categories e category.products, ma non puoi
avere informazioni aggiuntive.
nel caso has_many :through, invece:
in migration
def self.up
create_table ‘hotels’ do |t|
t.string :name
t.string :address
t.timestamps
end
create_table ‘clients’ do |t|
t.string :name
t.integer :age
t.timestamps
end
create_table ‘subscriptions’ do |t|
t.integer :hotel_id
t.integer :client_id
t.boolean :premiere
t.integer :price
t.datetime :entered_at
t.datetime :leaved_at
t.timestamps
end
end
models/hotel.rb
class Hotel < ActiveRecord::Base
has_many :subscriptions
has_many :clients, :through => :subscriptions
end
class Client < ActiveRecord::Base
belongs_to :subscriptions
belongs_to :hotels, :through => :subscriptions
end
class Subscription < ActiveRecord::Base
has_many :hotels
has_many :subscriptions
end
in questo caso devi creare un modello Subscription.
puoi ancora usare client.hotels e hotel.clients, ma puoi anche avere
informazioni aggiuntive, passando per subscriptions.
n.b. codice scritto di getto, non testato. prova, se non funziona chiedi
pure.
pietro
2009/1/25 Pietro G. [email protected]
t.string :qualcosa
t.column :category_id, :integermodels/product.rb
nel caso has_many :through, invece:
t.string :name
t.datetime :leaved_at
class Client < ActiveRecord::Basepuoi ancora usare client.hotels e hotel.clients, ma puoi anche avere
informazioni aggiuntive, passando per subscriptions.n.b. codice scritto di getto, non testato. prova, se non funziona chiedi
pure.
la tua relazione sarebbe molti a molti tra hotels e clients?
2009/1/8 Antonio C. [email protected]
2009/1/7 Mauro [email protected]:
…se volessi fare un collegamento molti a molti tra due tabelle?
Dovrei creare una terza tabella con le chiavi primarie delle due tabelle.Hai due possibilità. Puoi usare una join table oppure un join model
tramite l’opzione :through (particolarmente, se ti servono altri
campi).
Sto vedendo la guida
http://guides.rubyonrails.org/association_basics.htmlma il mio inglese
non e’ il massimo.
Se uso has_many :through devo creare la tabella di collegamento mentre
se
uso has_and_belong_to_many la terza tabella non la dovrei creare?
Ho capito male?