ho i seguenti tre modelli unirsi-rapportoRails Partecipa Modello selezionare le colonne comuni
class Book < ActiveRecord::Base
has_many :contributions, :dependent => :destroy
has_many :contributors, :through => :contributions
end
class Contributor < ActiveRecord::Base
has_many :contributions, :dependent => :destroy
has_many :books, :through => :contributions do
def this_is_my_contribution(book, join_attrs)
Contribution.with_scope(:create => join_attrs) {self << book}
end
end
end
class Contribution < ActiveRecord::Base
belongs_to :book
belongs_to :contributor
end
Poi, dopo l'inserimento di record nel Contributo unirsi modella, ho deciso che voglio fare una domanda su questo modello per recuperare tutte le libri e nomi di collaboratori come una query risultato come il seguente SQL equivalente
SELECT Contributions.*, Contributors.name, Books.name from Contributions
INNER JOIN Contributors ON Contributors.id = Contributions.contributors_id
INNER JOIN Books ON Books.id = Contributions.books_id
Ma in console irb, quando ho scritto questo,
Contribution.joins(:contributor, :book).select("contributors.name, books.name,
contributions.*")
ottengo il seguente output invece
Contribution Load (0.8ms) SELECT contributors.name, books.name, contributions.* FROM
"contributions" INNER JOIN "contributors" ON "contributors"."id" =
"contributions"."contributor_id" INNER
JOIN "books" ON "books"."id" = "contributions"."book_id"
=> #<ActiveRecord::Relation [#<Contribution id: 1, book_id: 1, contributor_id: 1, role:
"author", created_at: "2014-04-04 00:19:15", updated_at: "2014-04-04 00:19:15">, #
<Contribution id: 2, book_id: 2, contributor_id: 2, role: "Author", created_at: "2014-
04-05 06:20:34", updated_at: "2014-04-05 06:20:34">]>
non ho ricevuto alcun nome del libro e il nome del collaboratore in base alle chiavi INNER JOIN stranieri.
Non riuscivo a capire come l'istruzione SQL di RAILS si sbagliava molto quando questo è ciò che voglio intensamente.
Che cosa comprendo pienamente?
Grazie Sat's. Mi scuso per la risposta tardiva. Non ho la possibilità di provare il codice qui sopra. Ma dopo aver letto il link che hai fornito sopra, sta diventando più chiaro per me perché è necessario seguire questi passaggi. Grazie ancora! – awongCM