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'
これですべてのヘルプは大歓迎されます。
私はスコープが、@accounts = Account.find(のparams [:IDを])を追加。organizations.primaryはありません私のインデックスアクションで動作していないようです。なぜ(params [:id])をインクルードする必要がありますか? http:// localhost:3000/accountsのURLを介してすべてのプライマリ組織のリストを表示しようとしているため、URLにはIDがありません。 –
アカウントのすべてのプライマリ組織を表示しますか?その場合は、Organization.primary.allを使用してください。 –
いいえ、各アカウントには1つの主要組織しかありません。すべてのアカウントにすべてのアカウントを表示するにはどうすればよいですか? –