scambra/devise_invitable — Issue #160: Devise_Invitable and nested User Models
Author: funzelknut
Created: 2012-01-26 22:12:09+00:00
State: open

I have a User model and a model called Flat and a many_to_many association between them. Whenever a User is invited, I want him to be assigned to a Flat.

```ruby
class User < ActiveRecord::Base
has_and_belongs_to_many :flats

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
accepts_nested_attributes_for :flats, :allow_destroy => true
attr_accessible :email, :password, :password_confirmation, :remember_me, :flats_attributes
end

class Flat < ActiveRecord::Base
attr_accessible :name, :id, :users_attributes
has_and_belongs_to_many :users
accepts_nested_attributes_for :users, :allow_destroy => true
end

# View
= form_for :user, :url => { :action => "create", :controller=> "homes" }do |f|
%p
= f.label :email
= f.text_field :email

= f.fields_for :flats do |b|
= b.hidden_field :name

%p
= f.submit "Send an invitation"
```
In the Controller it is

```ruby
def create
@invite = User.invite!(params[:user])
end
```
Whenever I send an invitation, the log gives me

```ruby
Started POST "/homes" for 127.0.0.1 at 2012-01-26 22:55:30 +0100
[Barista] Compiling all scripts for barista
[Barista] Compiling all coffeescripts
[Barista] Copying all javascripts
Processing by HomesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XOrl9jBoR+RQ0+Hs4JmWJkvkz5OEh5Y0Li06lP8tMpo=", "nuser"=>{"email"=>"xxxx@xxx.xx", "flats"=>{"name"=>"xxxxxx"}}, "commit"=>"Send an invitation"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'ich@dus.de' LIMIT 1
WARNING: Can't mass-assign protected attributes: flats
(0.4ms) UPDATE "users" SET "invitation_sent_at" = '2012-01-26 21:55:30.392044', "updated_at" = '2012-01-26 21:55:30.392722' WHERE "users"."id" = 8
```
As far as I can tell, the attributes of each model are writable. Can anybody help?