2011-08-14 14 views
0

私の/ accounts/pageのデータベースに 'primary' = trueのaccount.organizationsのみを表示するにはどうすればよいですか?ここに私が今しようとしているもの:特定の属性を持つすべてのネストされたモデルを表示

class Account < ActiveRecord::Base 

    has_many :organizations 
    has_one :primary_organization, 
     :class_name => 'Organization', 
     :conditions => ['primary = ?', true] 

    accepts_nested_attributes_for :organizations 

end 

class Organization < ActiveRecord::Base 

    belongs_to :account 

    has_many :locations 
    has_one :primary_location, 
     :class_name => 'Location', 
     :conditions => ['primary = ?', true] 

    accepts_nested_attributes_for :locations 

end 

組織表には、ブール値のエントリを取る 'プライマリ'列があります。

class AccountsController < ApplicationController 

    def index 
     @accounts = account.organizations.where(:primary => true).all 
    end 

end 

そして最後に、インデックスビュー:

<h1>Account List</h1> 
    <table> 
    <% for account in @accounts %> 
    <tr> 
     <td><%= link_to account.organizations.name if 
         account.organizations.name %></td> 
    </tr> 
    <% end %> 
    </table> 

私は現在、このエラーを取得しています:

NameError in AccountsController#index 

undefined local variable or method `account' for #<AccountsController:0x1e160b0> 
Rails.root: C:/Documents and Settings/Corey Quillen/My  
Documents/rails_projects/shop_manager 

Application Trace | Framework Trace | Full Trace 
app/controllers/accounts_controller.rb:4:in `index' 

これですべてのヘルプは大歓迎されます。

答えて

0

いくつかの問題があります。あなたの最初の問題は、アカウントコントローラでアカウントを見つけることがないということです。それはあなたが持っている例外を得る理由です。最初にアカウントを読み込んでから組織を取得する必要があります。あなたがレール3のように見えるので、主要な組織の範囲を宣言することができます。

scope :primary, where(:primary => true) 

上記の範囲は、あなたがこれを言うことができる組織クラス内にある場合:

@accounts = Account.find(params[:id]).oranizations.primary 
+0

私はスコープが、@accounts = Account.find(のparams [:IDを])を追加。organizations.primaryはありません私のインデックスアクションで動作していないようです。なぜ(params [:id])をインクルードする必要がありますか? http:// localhost:3000/accountsのURLを介してすべてのプライマリ組織のリストを表示しようとしているため、URLにはIDがありません。 –

+0

アカウントのすべてのプライマリ組織を表示しますか?その場合は、Organization.primary.allを使用してください。 –

+0

いいえ、各アカウントには1つの主要組織しかありません。すべてのアカウントにすべてのアカウントを表示するにはどうすればよいですか? –

関連する問題