views:

51

answers:

1

I want to use fields_for on a subset of records in an association.

I have a Month model, which has_many :payments.

But in my form in my view I only want to have fields_for some of those payments. For example:

- fields_for @month.payments.large

This doesn't work.

Can I pass a set of records to fields_for, rather than the usual symbol (fields_for :payments) approach?

+1  A: 

You can add additional association for large payments, for example:

class Month < ActiveRecord::Base
  has_many :payments
  has_many :large_payments, :class_name => "Payment", :conditions => "value > 1000000"
end

After that you can use fields_for in common way:

- fields_for :large_payments

I think to encapsulate this logic on a model side is a better approach then in the view.

Voldy
Good thinking - that did the trick. I also had to add `accepts_nested_attributes_for :large_payments`. Thanks :)
nfm
You are right. And you should add `attr_accessible :large_payments_attributes`, if you protect your payment model from mass-assignment (good practice to protect).
Voldy