Re: [Rails] strange difference between has_one and has_many
by Steve Downey other posts by this author
Aug 30 2005 6:43AM messages near this date
[Rails] strange difference between has_one and has_many
|
Re: [Rails] strange difference between has_one and has_many
Jean-Christophe Michel wrote:
> Hi,
>
> I don't know rails enough to decide wether this is a bug or feature.
>
>
>
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.
>
>
>
You said there are many, so menu_texts (note the plural) returns an array.
> 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)
>
>
>
You said it has one, so menu_text (no plural) returns a single object.
> 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?
>
>
_______________________________________________
Rails mailing list
Rails@[...].org
http://lists.rubyonrails.org/mailman/listinfo/rails
Thread:
Jean-Christophe Michel
Steve Downey
Jean-Christophe Michel
|