ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-rails
ruby-rails
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved