Noob: Weird Association Question

Ok, so I’ve been trying to make this thing work for a couple of days now
without luck. I am just tarting to think that what I want to do is not
possible. The associations that I am trying to describe are:

Account:
has_many :groups
has_many :users
belongs_to :admin, :class_name => “User”
belongs_to :default_group, :class_name => “Group”

validates_presence_of :company_name, :max_users, :email

after_create :create_default_admin_and_group

User:
belongs_to :account
belongs_to :role
has_and_belongs_to_many :groups

validates_presence_of :first_name

before_create :account_max_users?
before_validation :generate_node_token, :if => “node_token.blank?”
before_validation :ensure_authentication_token, :if =>
“authentication_token.blank?”

before_destroy :is_group_admin?
before_destroy :is_account_admin?

Role:
has_many :users

validates_presence_of :name
validates_uniqueness_of :name

Group:
belongs_to :admin, :class_name => “User”
belongs_to :account
has_and_belongs_to_many :users

validates_presence_of :name
validates_uniqueness_of :name, :scope => :account_id

The most complicated association is the one between Account and Users,
and
Account and Groups. The thing is that an Account has many users and
groups
(each of them has account_id attribute), but at the same time, and
account
belongs to an User (which is the admin of the account) and belongs to a
Group (which is the default group of the account).

Right now, what I’m trying to do is to create an Account, so that will
call
the create_default_admin_and_group method which is basically something
like
this:

def create_default_admin_and_group
role = Role.find_or_create_by_name(“Admin”)

admin = self.create_admin(:ebmail      => email,
                          :first_name => company_name,
                          :password   => generate_random_password,
                          :role       => role)

self.build_default_group(:name   => company_name,
                         :admin  => admin)

end

It should create an “Admin” role if it doesn’t exist yet and then create
the
admin (User class) of the account. Before creating the admin the
account_max_users method is executed in order to know if we can add more
users to the account. The problem comes here:

def account_max_users?
!self.account.max_users?
end

This method should call the max_users? method from the Account model
which
is something like:

def max_users?
self.users.count == self.max_users
end

where max_users is an attribute of an account, but for some weird reason
self.account is nil in the account_max_users? method. It looks like the
following code:

admin = self.create_admin(:email => email,
:first_name => company_name,
:password => generate_random_password,
:role => role)

doesn’t seem to set role_id and account_id correctly. I’ve checked that
printing out the object just before “!self.account.max_users?” is
executed
and this is the result:

#User:0x00000103ce97e0 {
:id => nil,
:email => “[email protected]”,
:encrypted_password =>
“$2a$10$KjaFu/fTqajr/f650oJ/q…essukILBNVkOKZKr6fFt23JqV6lrT6”,
:password_salt => “$2a$10$KjaFu/fTqajr/f650oJ/q.”,
:reset_password_token => nil,
:remember_token => nil,
:remember_created_at => nil,
:sign_in_count => 0,
:current_sign_in_at => nil,
:last_sign_in_at => nil,
:current_sign_in_ip => nil,
:last_sign_in_ip => nil,
:authentication_token => “XYxHY0eC8GHJg-pbp3CB”,
:created_at => nil,
:updated_at => nil,
:node_token => “dd4f30ae8641d441bd666f03b4a653d6”,
:first_name => “Company-1”,
:last_name => nil,

  •          :account_id => nil,*
    
  •             :role_id => nil*
    

}

Is there any reason why this is happening? Are those associations
correct?
Am I doing something wrong?

Thanks in advance.

I’m using Rails 3.0.5 and Ruby 1.9.2-head. If you need any other
information
just tell me.