Nested Form Models Account -> User

346 views
Skip to first unread message

joshc

unread,
Oct 9, 2011, 8:11:48 PM10/9/11
to Devise
Hi,

I've been hitting my head against the wall for a few hours trying
various solutions found in the message board but none seem to work for
me.

I have (what I think) is a simple problem. (Rails 3.1 with Devise)

I have 2 models Account and User
Account has_many :users
User belongs_to :account

When users register for the site they can "create" an account say
Account is analogous to "family" "company" "organization" etc. Where
the Users are members of that account organization. The first user
would later "invite" or add" other Users into the Account.

I'm trying to customize the register form to allow for a field to name
the account to be added.

in my user model i have:

belongs_to :account
attr_accessible :email, :password, :password_confirmation, :remember_me
accepts_nested_attributes_for :account

in my account model I just have:
has_many :users

In my form registrations/new.html.erb:
...
<%= devise_error_messages! %>

<% f.fields_for :account do |builder| %>
<div class="field">
<%= builder.label :account %><br />
<%= builder.text_field :account %>
</div>
<% end %>

<div><%= f.label :email %><br />
<%= f.email_field :email %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
...

I don't get any errors, but the field to create the account never
appears. I've tried mucking with the routes but that doesn't have any
effect, I've tried including the following in the User model as well
and no avail.
def with_account
self.accounts.build
self
end

Am I going to have to override the devise controller to do this?

thanks in advance for your guidance

Carlos Antonio da Silva

unread,
Oct 12, 2011, 4:34:33 PM10/12/11
to plataforma...@googlegroups.com
You have to make sure there is a new account for the current user object being rendered, otherwise fields_for won't render anything. Try something like:

    <% f.object.build_account unless f.object.account %>
    <% f.fields_for :account do |builder| %>

I think that should work.
--
At.
Carlos Antonio

mcbsys

unread,
Nov 29, 2011, 5:52:54 PM11/29/11
to plataforma...@googlegroups.com
Josh,

This is exactly what I am trying to do with Rails 3.1 and Devise 1.48. Did you get it to work?

One thing you might need is to add :account_attributes to your attr_accessible.

However, I'm not even sure that accepts_nested_attributes_for will work this way. If I understand correctly, that is supposed to go on the parent model, in this case Account. I've tried putting it on the User model as you have, then running the following RSpec test (in spec/models/user_spec.rb):

    it "should work with accepts_nested_attributes_for" do
      @userwithaccount = { :email => "FakeEmailForGroup",
                           :password => "testpass",
                           :account_attributes => { :name => "Test Account" } }
      User.create!(@userwithaccount)
    end

That fails with "ActiveRecord::RecordInvalid: Validation failed: Account can't be blank".

My hunch is that accepts_nested_attributes_for tries to create the "local" model first, assuming it is the parent, then tries to link the "foreign" model to it. So it's trying to create the User object, but it can't because User requires a link to Account, i.e. Account must be created first.

Seems like if it doesn't work at the model level from a test, it's not going to work from a form.

Any further info on how to solve this would be appreciated!

Thanks,
Mark Berry

mcbsys

unread,
Dec 15, 2011, 10:23:04 PM12/15/11
to plataforma...@googlegroups.com
I think I've got this working. See the answer I posted on StackOverflow:

http://stackoverflow.com/questions/8305120/use-both-account-and-user-tables-with-devise/8529340#8529340

Mark Berry
Reply all
Reply to author
Forward
0 new messages