[Rails] strange difference between has_one and has_many
by Jean-Christophe Michel other posts by this author
Aug 29 2005 4:40AM messages near this date
Re: [Rails] Active Record problem
|
Re: [Rails] strange difference between has_one and has_many
Hi,
I don't know rails enough to decide wether this is a bug or feature.
I have two models, Menu and MenuText (many langs, so many texts per
menu). (see sql below)
If I define has_many:
class Menu < ActiveRecord::Base
has_many :menu_texts, :conditions => ['lang = ?', 'fr']
end
class MenuText < ActiveRecord::Base
belongs_to :menu
end
then
Menu.find(1).menu_texts.first shows the unique menu_text associated to
the menu.
no pb, everything works.
But if I change to has_one:
class Menu < ActiveRecord::Base
has_one :menu_text, :conditions => ['lang = ?', 'fr']
end
then
Menu.find(1).menu_text raises an error (array to string conversion)
It's due to the difference of treatment that conditions receive in both
associations code:
active_record/associations/has_many_association.rb:
@finder_sql << " AND #{interpolate_sql(@conditions)}" if @conditions
active_record/associations/has_one_association.rb, li 68:
#{@options[:conditions] ? " AND " + @options[:conditions].
Why does has_one treat conditions differently?
Isn't it a bug ?
SQL:
CREATE TABLE menus (
id BIGSERIAL NOT NULL,
-- Some statistics about the user data
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
...
PRIMARY KEY (id),
FOREIGN KEY (parent_id) REFERENCES menus (id) ON DELETE RESTRICT
);
CREATE TABLE menu_texts (
id BIGSERIAL NOT NULL,
menu_id BIGINT NOT NULL,
-- Real information: lang, title
lang CHARACTER (2) NOT NULL DEFAULT 'fr',
title CHARACTER VARYING (200) NOT NULL,
FOREIGN KEY (menu_id) REFERENCES menus (id) ON DELETE CASCADE
);
--
Jean-Christophe Michel
_______________________________________________
Rails mailing list
Rails@[...].org
http://lists.rubyonrails.org/mailman/listinfo/rails
Thread:
Jean-Christophe Michel
Steve Downey
Jean-Christophe Michel
|