Hello Everyone,
I''m beginning to get into rails and have started facing some problems.
I have a basic habtm relationship of users and accounts. Basically
accounts are companies and users are users of the application. What
I''m trying to do is take the account scaffold and the user create with
the account create. Basically the form is the combination of user/new
and account/new. I''m using ModelSecurity for the user model.
The error I''m getting is:
undefined method `save'' for User:Class
But there may be more.
Here is what I have. Am I even close?:
def new
@account = Account.new
@user = User.new
end
def create
user = User.find(:all, :conditions => ["email = ?",
@params[''email'']])
# Tests if user already exists
if user
# If user doesn''t exist, create them.
else
p = @params[''user'']
@user = User.new(p)
end
#Here we save the account and user together.
@account = Account.new(params[:account])
@account.users << user
if @account.users.save
send_confirmation_email(user)
@email = params[:email]
flash[:notice] = ''Account was successfully created.''
redirect_to :action => ''list''
else
render :action => ''new''
end
end
Thanks!
-jeff
you are using User.find(:all, ...) which results in an array of users (or an empty array if none are found), even though you are only looking for one. try: User.find(:first, :conditions => ["email = ?", @params[:email]]) or even User.find_by_email(@params[:email]) (a dyunamic finder created by Rails) On 10/31/05, Jeff Blasius <jeff.blasius-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello Everyone, > I''m beginning to get into rails and have started facing some problems. > I have a basic habtm relationship of users and accounts. Basically > accounts are companies and users are users of the application. What > I''m trying to do is take the account scaffold and the user create with > the account create. Basically the form is the combination of user/new > and account/new. I''m using ModelSecurity for the user model. > > The error I''m getting is: > undefined method `save'' for User:Class > > But there may be more. > > Here is what I have. Am I even close?: > def new > @account = Account.new > @user = User.new > end > > def create > user = User.find(:all, :conditions => ["email = ?", @params[''email'']]) > # Tests if user already exists > if user > # If user doesn''t exist, create them. > else > p = @params[''user''] > @user = User.new(p) > end > > #Here we save the account and user together. > @account = Account.new(params[:account]) > @account.users << user > if @account.users.save > send_confirmation_email(user) > @email = params[:email] > flash[:notice] = ''Account was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > > > > Thanks! > -jeff > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thank You for the reply. I think I''m a bit confused. Let me explain
the situation:
I have a user/new form which allows me to add users. I also have a
account/new form which is essentially the user/new form combined with
account information. users and accounts are a habtm relationship. The
user/new form is simple, it''s the account/new form I''m having
trouble
with.
* if users enter a registered, active, and valid email
address/password combination they should be logged in and the account
information should be added and linked with that account.
* if users enter an unregistered email address the user should be
created and the account should be linked with the new user.
Obviously these two conditions have different validation requirements.
I''m just not sure how to structure this transaction.
Any help would be appreciated. Clearly I''m not a programmer. I build
systems for them :)
Thanks,
jeff
On 11/1/05, Chris Hall
<christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> you are using User.find(:all, ...) which results in an array of users (or
an
> empty array if none are found), even though you are only looking for one.
> try:
>
> User.find(:first, :conditions => ["email = ?",
@params[:email]]) or even
> User.find_by_email(@params[:email]) (a dyunamic finder created by Rails)
>
>
>
> On 10/31/05, Jeff Blasius
<jeff.blasius-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > Hello Everyone,
> > I''m beginning to get into rails and have started facing some
problems.
> > I have a basic habtm relationship of users and accounts. Basically
> > accounts are companies and users are users of the application. What
> > I''m trying to do is take the account scaffold and the user
create with
> > the account create. Basically the form is the combination of user/new
> > and account/new. I''m using ModelSecurity for the user model.
> >
> > The error I''m getting is:
> > undefined method `save'' for User:Class
> >
> > But there may be more.
> >
> > Here is what I have. Am I even close?:
> > def new
> > @account = Account.new
> > @user = User.new
> > end
> >
> > def create
> > user = User.find(:all, :conditions => ["email = ?",
@params[''email'']])
> > # Tests if user already exists
> > if user
> > # If user doesn''t exist, create them.
> > else
> > p = @params[''user'']
> > @user = User.new(p)
> > end
> >
> > #Here we save the account and user together.
> > @account = Account.new(params[:account])
> > @account.users << user
> > if @account.users.save
> > send_confirmation_email(user)
> > @email = params[:email]
> > flash[:notice] = ''Account was successfully
created.''
> > redirect_to :action => ''list''
> > else
> > render :action => ''new''
> > end
> > end
> >
> >
> >
> >
> > Thanks!
> > -jeff
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
--
Jeff Blasius / jeff.blasius-LrD5EImo2rg@public.gmane.org
Phone: (203)432-9940 51 Prospect Rm. 011
High Performance Computing (HPC)
UNIX Systems Administrator, WorkStation Support (WSS)
Yale University Information Technology Services (ITS)