Add additional result to the result of getting the Collection Proxy association
I have two models,
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to: user
end
I am using formtastic gem
, consider in edit
action users_controller
. All required attributes user
and associated posts
form attributes will be filled in with a formal form
code:
<%= semantic_form_for @user do |f| %>
<%= f.input :name %>
<%= f.inputs :posts do |p| %>
<%= p.input :title %>
<%= p.input :comment %>
<% end %>
<% end %>
For example, I have @user
two posts
. by doing @user.posts
, the result will be similar.
[
[0] #<Post:0x0000000aa53a20> {
:id => 3,
:title => 'Hello World',
:comment => 'Long text comes here'
},
[1] #<Post:0x0000000aa53a41> {
:id => 5,
:title => 'Hello World 2',
:comment => 'Long text comes here too'
}
]
Thus, the form will contain two fields for editing.
Actually, I want another blank post form before these two posts.
This can be easily achieved by inserting a new empty object post
into the result @object.posts
at position 0.
So the result @object.posts
I want should look exactly like,
[
[0] #<Post:0x0000000aa53a50> {
:id => nil,
:title => nil,
:comment => nil
},
[1] #<Post:0x0000000aa53a20> {
:id => 3,
:title => 'Hello World',
:comment => 'Long text comes here'
},
[2] #<Post:0x0000000aa53a41> {
:id => 5,
:title => 'Hello World 2',
:comment => 'Long text comes here too'
}
]
Any solutions to get this structure from @user.posts
?
source to share