Hi all,
I have a Resources and Assignments table with
many-to-many relationship.
In order to create a new assignment, here is what I do
in my controller class:
@assignment = Assignment.new(@params[:assignment])
resource = Resource.find(:first, :condition =>
"id=#{session[''resource_id'']}")
@assignment.resource[0] = resource
if @assignment.save
logger.info("Successful creation")
else
logger.info("Unsuccessful creation")
end
For some reason, the @assignment.save call is
returning false and it does not save anything.
Basically I have a form that includes all the fields
for a new assignment. Once the user submits the
request, it would call the above code to get the
associated resource which is logged into the system.
It is supposed to assign that resource to the newly
created assignment and the save the new assignment
along with an entry into the join
table(resources_assignments) table.
Any ideas on what is wrong with the code snippet above
and how I can fix it would be greatly appreciate it.
Thanks in advance,
Payam.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
I''m not sure exactly what the problem is, but here are some thoughts on the code you posted:> @assignment = Assignment.new(@params[:assignment]) > resource = Resource.find(:first, :condition => > "id=#{session[''resource_id'']}")I think you should also be able to use this to simplify: resource = Resource.find(session[''resource_id''])> @assignment.resource[0] = resource >The above line looks a little fishy to me (I''ve never used a collection like that). I would do this instead: @assignment.resources << resource> if @assignment.save > logger.info("Successful creation") > else > logger.info("Unsuccessful creation") > end >If you want to see exactly why it''s failing to save, try @assignment.save! which will raise an error for you so you can see why it failed.> Thanks in advance, > Payam.Good luck, Duane Johnson (canadaduane)
Payam Fard wrote:>Any ideas on what is wrong with the code snippet above >and how I can fix it would be greatly appreciate it. > >I wonder if since the @assignment.save is what is failing if the problem is not necessarily related to the relationship. However, I did notice that you said your table was called "resources_assignments" I believe it should actually be "assignments_resources" as the names should be in alphabetical order, I think. Then you can check that table to see if anything is getting inserted before the @assignment.save. Try using the code Duane posted and fix the table naming issue and then see what kinds of errors or successes you have and we can take another look at it. I battled with a many-to-many relationship and its code for quite a while, so I know that it can be confusing. -- Alex Ezell
Thanks for your reply. Here is how I have changed my
code, even though there is still no change to the
outcome of my program. @assignment.save! is not
throwing any exceptions or errors either. Here is the
modified version. Evertime I submit, I would go to the
else. I made a mistake in my original post, my join
table is called assignments_resources. When I print
the contents of assignment, I am getting the correct
values as well as the associated resource.
def create
@assignment = Assignment.new(@params[:assignment])
resource = Resource.find(session[''resource_id''])
@assignment.resources << resource
logger.warn(@assignment)
if @assignment.save!
logger.warn("Inside if of assignment.save!!!")
redirect_to :action => ''list''
else
logger.warn("Inside else of assignment.save!!!")
render_action ''new''
end
end
Thanks,
Payam.
--- Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> I''m not sure exactly what the problem is, but here
> are some thoughts
> on the code you posted:
>
> > @assignment = Assignment.new(@params[:assignment])
> > resource = Resource.find(:first, :condition =>
> > "id=#{session[''resource_id'']}")
>
> I think you should also be able to use this to
> simplify:
> resource = Resource.find(session[''resource_id''])
>
> > @assignment.resource[0] = resource
> >
> The above line looks a little fishy to me (I''ve
> never used a
> collection like that). I would do this instead:
> @assignment.resources << resource
>
> > if @assignment.save
> > logger.info("Successful creation")
> > else
> > logger.info("Unsuccessful creation")
> > end
> >
> If you want to see exactly why it''s failing to save,
> try
> @assignment.save! which will raise an error for you
> so you can see
> why it failed.
>
> > Thanks in advance,
> > Payam.
>
> Good luck,
> Duane Johnson
> (canadaduane)
>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
On Jun 21, 2005, at 9:00 AM, Payam Fard wrote:> Thanks for your reply. Here is how I have changed my > code, even though there is still no change to the > outcome of my program. @assignment.save! is not > throwing any exceptions or errors either. Here is theHmmm, seems strange. What do your models look like? Duane Johnson (canadaduane)
Here is my model: class Assignment < ActiveRecord::Base has_and_belongs_to_many :resources end class Resource < ActiveRecord::Base has_and_belongs_to_many :assignments end --- Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 21, 2005, at 9:00 AM, Payam Fard wrote: > > Thanks for your reply. Here is how I have changed > my > > code, even though there is still no change to the > > outcome of my program. @assignment.save! is not > > throwing any exceptions or errors either. Here is > the > > Hmmm, seems strange. What do your models look like? > > Duane Johnson > (canadaduane) > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Hi all, I am having a problem with inserting a new record into a table with many-to-many relationship with another table. For some reason, my call to save and save! fail. I am looking at the development.log file, but I do not see the insert sql that ActiveRecord is trying to execute. I am getting other stuff written to the logs, but it looks like it does not write everything. Is there any way to have ActiveRecord write this sql into the log to facilitate debugging my problem? Is there a setting I can play around with to have it write more things to the log? Thanks in advance, Payam. ____________________________________________________ Yahoo! Sports Rekindle the Rivalries. Sign up for Fantasy Football http://football.fantasysports.yahoo.com
On Jun 21, 2005, at 9:00 AM, Payam Fard wrote:> Thanks for your reply. Here is how I have changed my > code, even though there is still no change to the > outcome of my program. @assignment.save! is not > throwing any exceptions or errors either. Here is the > modified version. Evertime I submit, I would go to the > else. I made a mistake in my original post, my join > table is called assignments_resources. When I print > the contents of assignment, I am getting the correct > values as well as the associated resource. > > def create > @assignment = Assignment.new(@params[:assignment])Here''s the issue: you need to save your @assignment object before you add the resource. Otherwise, there is no @assignment.id that your resource.assignment_id can be set to. Perhaps you could re-arrange your method, or else use Assignment.create instead. (Assignment.create would do the same thing as Assignment.new.save).> resource = Resource.find(session[''resource_id'']) > @assignment.resources << resource > logger.warn(@assignment) > if @assignment.save! > logger.warn("Inside if of assignment.save!!!") > redirect_to :action => ''list'' > else > logger.warn("Inside else of assignment.save!!!") > render_action ''new'' > end > end > > Thanks, > Payam.Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks again for your reply. Is this the change you
are proposing in your email? I have added
@assignment.save before getting the resources and
adding resources to the assinment. What happens is
that a record gets inserted into the assignment table,
but the corresponding entry into the join table does
not get inserted!!! I still do not get any error!
Did I follow your suggestion correctly?
@assignment Assignment.new(@params[:assignment])
@assignment.save #Added based on your email
resource = Resource.find(session[''resource_id''])
@assignment.resources << resource
logger.warn(@assignment)
if @assignment.save!
logger.warn("Inside if of assignment.save!!!")
redirect_to :action => ''list''
else
logger.warn("Inside else of assignment.save!!!")
render_action ''new''
end
end
--- Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
> On Jun 21, 2005, at 9:00 AM, Payam Fard wrote:
>
> > Thanks for your reply. Here is how I have changed
> my
> > code, even though there is still no change to the
> > outcome of my program. @assignment.save! is not
> > throwing any exceptions or errors either. Here is
> the
> > modified version. Evertime I submit, I would go to
> the
> > else. I made a mistake in my original post, my
> join
> > table is called assignments_resources. When I
> print
> > the contents of assignment, I am getting the
> correct
> > values as well as the associated resource.
> >
> > def create
> > @assignment > Assignment.new(@params[:assignment])
>
> Here''s the issue: you need to save your @assignment
> object before you
> add the resource. Otherwise, there is no
> @assignment.id that your
> resource.assignment_id can be set to. Perhaps you
> could re-arrange
> your method, or else use Assignment.create instead.
>
> (Assignment.create would do the same thing as
> Assignment.new.save).
>
> > resource >
Resource.find(session[''resource_id''])
> > @assignment.resources << resource
> > logger.warn(@assignment)
> > if @assignment.save!
> > logger.warn("Inside if of
> assignment.save!!!")
> > redirect_to :action => ''list''
> > else
> > logger.warn("Inside else of
> assignment.save!!!")
> > render_action ''new''
> > end
> > end
> >
> > Thanks,
> > Payam.
>
> Duane Johnson
> (canadaduane)
>
>
> > _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports.yahoo.com
Also on the same note, given I can get the following solution working, I guess I have to run it as part of a transaction so that either both records get inserted or none, right? --- Payam Fard <p_fard_2000-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Thanks again for your reply. Is this the change you > are proposing in your email? I have added > @assignment.save before getting the resources and > adding resources to the assinment. What happens is > that a record gets inserted into the assignment > table, > but the corresponding entry into the join table does > not get inserted!!! I still do not get any error! > > Did I follow your suggestion correctly? > > @assignment > Assignment.new(@params[:assignment]) > > @assignment.save #Added based on your email > > resource = Resource.find(session[''resource_id'']) > @assignment.resources << resource > logger.warn(@assignment) > if @assignment.save! > logger.warn("Inside if of assignment.save!!!") > redirect_to :action => ''list'' > else > logger.warn("Inside else of > assignment.save!!!") > render_action ''new'' > end > end > > > --- Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Jun 21, 2005, at 9:00 AM, Payam Fard wrote: > > > > > Thanks for your reply. Here is how I have > changed > > my > > > code, even though there is still no change to > the > > > outcome of my program. @assignment.save! is not > > > throwing any exceptions or errors either. Here > is > > the > > > modified version. Evertime I submit, I would go > to > > the > > > else. I made a mistake in my original post, my > > join > > > table is called assignments_resources. When I > > print > > > the contents of assignment, I am getting the > > correct > > > values as well as the associated resource. > > > > > > def create > > > @assignment > > Assignment.new(@params[:assignment]) > > > > Here''s the issue: you need to save your > @assignment > > object before you > > add the resource. Otherwise, there is no > > @assignment.id that your > > resource.assignment_id can be set to. Perhaps you > > could re-arrange > > your method, or else use Assignment.create > instead. > > > > (Assignment.create would do the same thing as > > Assignment.new.save). > > > > > resource > > Resource.find(session[''resource_id'']) > > > @assignment.resources << resource > > > logger.warn(@assignment) > > > if @assignment.save! > > > logger.warn("Inside if of > > assignment.save!!!") > > > redirect_to :action => ''list'' > > > else > > > logger.warn("Inside else of > > assignment.save!!!") > > > render_action ''new'' > > > end > > > end > > > > > > Thanks, > > > Payam. > > > > Duane Johnson > > (canadaduane) > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > ____________________________________________________ > > Yahoo! Sports > Rekindle the Rivalries. Sign up for Fantasy Football > > http://football.fantasysports.yahoo.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Discover Yahoo! Use Yahoo! to plan a weekend, have fun online and more. Check it out! http://discover.yahoo.com/