On 23 July 2013 08:23, Colin Law <
cla...@googlemail.com> wrote:
> On 21 July 2013 18:28, James Gray <
zaa...@gmail.com> wrote:
>> Hi all,
>>
>> In my controller I am doing the following to populate a nested form for a
>> has_many through association:
>>
>> def new
>> @specification = Specification.new
>>
>> Component.find_each.each do |component|
>> @specification.component_specifications.build(:component_id =>
>>
component.id)
>
> Don't use .new or .build in the view, do it in the controller so that
> everything is prepared before rendering the view. The view code
> should only be concerned with displaying the data.
Sorry, I seem to have suffered from brain fade when I wrote the above
code, it is in the controller. You might want to put the second part
in a method of the Specification model however. I am not sure why you
are using find_each but that is a different issue.
>
>> end
>>
>> The idea being whenever someone creates or edits a form, it will be
>> populated with all components at that time so that when they save those
>> components that specification will be associated with the newly created
>> specification. The problem I am having is I can't work out how to pass
>> component name to display in my form for the as yet nonexistent
>> component_specification as it is not accessible through the
>> ComponentSpecification model.
>
> I have no idea what you mean by the last sentence. But as I said
> above you should be doing this in the controller anyway.
I still don't understand what the problem you are describing is here
though. Why can't you just say
@specification.component_specifications?
Colin