cat album.rb class Album < ActiveRecord::Base has_one :format, :class_name => "Format", :foreign_key => "format_key" end class Format < ActiveRecord::Base set_table_name "album_format" end $ script/console Loading development environment. irb(main):001:0> a=Album.find(300) => #<Album:0x405fe2d4 @attributes={ ..., "format_key"=>"1", ... }> irb(main):002:0> a.format => nil ============================================================ # select format_key from albums where id=300; format_key ------------ 1 (1 row) # select * from album_format where format_key = 1; format_key | file_extension | description ------------+----------------+------------------ 1 | mp3 | MP3 encoded file (1 row) ============================================================ I have no idea what isn''t quite right... or how to (reasonably) troubleshoot it. craig
> class Album < ActiveRecord::Base > has_one :format, :class_name => "Format", :foreign_key => "format_key" > end> I have no idea what isn''t quite right... or how to (reasonably) > troubleshoot it.Try changing the has_one to a belongs_to. Caleb
Craig, On 21.4.2005, at 17:46, craig duncan wrote:> cat album.rb > > class Album < ActiveRecord::Base > has_one :format, :class_name => "Format", :foreign_key => > "format_key" > end > > class Format < ActiveRecord::Base > set_table_name "album_format" > end > > $ script/console > Loading development environment. > irb(main):001:0> a=Album.find(300) > => #<Album:0x405fe2d4 @attributes={ ..., "format_key"=>"1", ... }> > irb(main):002:0> a.format > => nil > > ============================================================> > # select format_key from albums where id=300; > format_key > ------------ > 1 > (1 row) > > # select * from album_format where format_key = 1; > format_key | file_extension | description > ------------+----------------+------------------ > 1 | mp3 | MP3 encoded file > (1 row) > > ============================================================> > I have no idea what isn''t quite right... or how to (reasonably) > troubleshoot it.Either your foreign key is in the wrong table or (more probably) you''re using the wrong association method. When the foreign key field is in the current table/model, it needs to use belongs_to. See http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html for more info. //jarkko> > craig > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Caleb Tennis wrote:>>class Album < ActiveRecord::Base >> has_one :format, :class_name => "Format", :foreign_key => "format_key" >>end > > >>I have no idea what isn''t quite right... or how to (reasonably) >>troubleshoot it. > > > Try changing the has_one to a belongs_to. > > Calebirb(main):002:0> a.format ActiveRecord::StatementInvalid: ERROR: column "id" does not exist : SELECT * FROM album_format WHERE id = 1 LIMIT 1 # cat album.rb class Album < ActiveRecord::Base belongs_to :format, :class_name => "Format", :foreign_key => "format_key" end class Format < ActiveRecord::Base set_table_name "album_format" end
> ActiveRecord::StatementInvalid: ERROR: column "id" does not exist > > : SELECT * FROM album_format WHERE id = 1 LIMIT 1 > > # cat album.rb > > class Album < ActiveRecord::Base > belongs_to :format, :class_name => "Format", :foreign_key => > "format_key" endRight. The "format_key" in the Album class points to the "id" field of the Format class (by default). Since album_format.id doesn''t exist, you get an error. You''ll either need to specify in Format that your primary_key isn''t called "id" ( set_primary_key "format_key" ), or change the "format_key" field to "id".> > class Format < ActiveRecord::Base > set_table_name "album_format" > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Craig, On 21.4.2005, at 18:09, craig duncan wrote:> Caleb Tennis wrote: >>> class Album < ActiveRecord::Base >>> has_one :format, :class_name => "Format", :foreign_key => >>> "format_key" >>> end >>> I have no idea what isn''t quite right... or how to (reasonably) >>> troubleshoot it. >> Try changing the has_one to a belongs_to. >> Caleb > > irb(main):002:0> a.format > ActiveRecord::StatementInvalid: ERROR: column "id" does not exist > : SELECT * FROM album_format WHERE id = 1 LIMIT 1You need to say in your model definition that the primary key field is something else than id. You can do that by overwriting ActiveRecord::Base#primary_key(). I''m not sure if there''s a magic function to do it like there is for table name (set_table_name). These are covered in the api docs (http://rails.rubyonrails.com/classes/ActiveRecord/Base.html) but primary_key is not that well documented.> class Format < ActiveRecord::Base > set_table_name "album_format"def primary_key() "format_key" end //jarkko> end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> def primary_key() > "format_key" > endNote that this doesn''t work (at least, it didn''t as of 0.11). You must either do: set_primary_key "format_key" or: def self.primary_key "format_key" end
Caleb Tennis wrote:>>ActiveRecord::StatementInvalid: ERROR: column "id" does not exist >> >>: SELECT * FROM album_format WHERE id = 1 LIMIT 1 >> >># cat album.rb >> >>class Album < ActiveRecord::Base >> belongs_to :format, :class_name => "Format", :foreign_key => >>"format_key" end > > > Right. The "format_key" in the Album class points to the "id" field of the > Format class (by default). Since album_format.id doesn''t exist, you get an > error. > > You''ll either need to specify in Format that your primary_key isn''t called > "id" ( set_primary_key "format_key" ), or change the "format_key" field to > "id". > > >>class Format < ActiveRecord::Base >> set_table_name "album_format" >>endThank you (Jarrko too, although you''ll have to work on your draw). That works. But... it seems that my saying that :foreign_key => "format_key" isn''t being utilized. If i take it out, though, the query doesn''t work. Hmmm... not DRY. craig
On 21.4.2005, at 18:33, craig duncan wrote:> Caleb Tennis wrote: >>> ActiveRecord::StatementInvalid: ERROR: column "id" does not exist >>> >>> : SELECT * FROM album_format WHERE id = 1 LIMIT 1 >>> >>> # cat album.rb >>> >>> class Album < ActiveRecord::Base >>> belongs_to :format, :class_name => "Format", :foreign_key => >>> "format_key" end >> Right. The "format_key" in the Album class points to the "id" field >> of the Format class (by default). Since album_format.id doesn''t >> exist, you get an error. >> You''ll either need to specify in Format that your primary_key isn''t >> called "id" ( set_primary_key "format_key" ), or change the >> "format_key" field to "id". >>> class Format < ActiveRecord::Base >>> set_table_name "album_format" >>> end > > Thank you (Jarrko too, although you''ll have to work on your draw). > That works. But... it seems that my saying that :foreign_key => > "format_key" isn''t being utilized. If i take it out, though, the > query doesn''t work. Hmmm... not DRY.It means that format_key is the foreign key used in *albums* table. Otherwise rails assumes that there is a field called "format_id" in albums (name of the other model + _id). So it''s there for a reason in your case. //jarkko> > craig > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails