From: Phrogz Date: 2007-11-22T06:50:07+09:00 Subject: Re: AR: many to many with attributes On Nov 21, 7:12 am, Christian Kerth wrote: > > 1) Do your models look like this: > >http://matthewman.net/2006/01/06/rails-activerecord-goes-through/ > > @1) yeah, i have set up the models with :through associations. The all > work. My question, which I didn't explain well, was: did you create a Model for the many-to-many table? > > 2) This is a rails question, not a Ruby one. Try the rails list. > > @2) Why is this a Rails question? I'm using ActiveRecord without > Rails... Ah, OK then. As I suspect you know, most people using AR are using Rails. My apologies for the assumption. Nonetheless, given that fact, that list seems most likely to have people able to answer your question. ...if this doesn't answer it: C:\tmp\db>sqlite3 development.sqlite3 SQLite version 3.5.2 Enter ".help" for instructions sqlite> .dump BEGIN TRANSACTION; CREATE TABLE schema_info (version integer); INSERT INTO "schema_info" VALUES(3); CREATE TABLE books ("id" INTEGER PRIMARY KEY NOT NULL, "author" varchar(255) DEFAULT NULL); CREATE TABLE languages ("id" INTEGER PRIMARY KEY NOT NULL, "langcode" varchar(255) DEFAULT NULL); CREATE TABLE book_publishings ("book_id" integer DEFAULT NULL, "language_id" integer DEFAULT NULL, "title" varchar(255) DEFAULT NULL); COMMIT; C:\tmp\app\models>type *.rb book.rb class Book < ActiveRecord::Base has_many :book_publishings has_many :languages, :through=>:book_publishings end book_publishing.rb class BookPublishing < ActiveRecord::Base belongs_to :book belongs_to :language end language.rb class Language < ActiveRecord::Base has_many :book_publishings has_many :books, :through=>:book_publishings end c:\tmp> ruby script\console Loading development environment. >> cat_book = Book.create :author=>'me' => #>, @new_record_before_save=true, @attributes={"author"=>"me", "id"=>1}, @new_record=false> >> english = Language.create :langcode=>'en' => #>, @new_record_before_save=true, @attributes={"id"=>1, "langcode"=>"en"}, @new_record=false> >> german = Language.create :langcode=>'de' => #>, @new_record_before_save=true, @attributes={"id"=>2, "langcode"=>"de"}, @new_record=false> >> BookPublishing.create :book=>cat_book, :language=>english, :title=>'My Cat is Awesome' => #>, @new_record_before_save=true, @attributes={"author"=>"me", "id"=>1}, @new_record=false>, @errors=#>, @language=#>, @new_record_before_save=true, @attributes={"id"=>1, "langcode"=>"en"}, @new_record=false>, @ attributes={"title"=>"My Cat is Awesome", "id"=>1, "language_id"=>1, "book_id"=>1}, @new_record=false> >> BookPublishing.create :book=>cat_book, :language=>german, :title=>'Meine Katze ist genial' => #>, @new_record_before_save=true, @attributes={"author"=>"me", "id"=>1}, @new_record=false>, @errors=#>, @language=#>, @new_record_before_save=true, @attributes={"id"=>2, "langcode"=>"de"}, @new_record=false>, @attributes={"title"=>"Meine Katze ist genial", "id"=>2, "language_id"=>2, "book_id"=>1}, @new_record=false> >> cat_book.languages.map &:langcode => ["en", "de"] >> cat_book.book_publishings.map &:title => ["My Cat is Awesome", "Meine Katze ist genial"] >> cat_book.book_publishings.find_by_language_id( english.id ) => #"My Cat is Awesome", "language_id"=>"1", "book_id"=>"1"}> Note that due to the crazy magic of AR, that last line is a single SQL query: SELECT * FROM book_publishings WHERE (book_publishings.book_id = 1) AND (book_publishings."language_id" = 1) LIMIT 1