Forum: Ruby on Rails Saving a collection field with <<

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6283a59f2a604f29df2a4a68b320cc1d?d=identicon&s=25 Chris Johanesen (Guest)
on 2006-02-23 07:44
I have habtm  relationship between articles and contributors.
Everything's working fine, except when I create a new article Rails
doesn't seem to save the contributors that belong to it into
articles_contributors. This is the controller (that I pilfered from
typo):

def new
    @article = Article.new(params[:article])
    [...]
    if request.post?
      @article.contributors.clear
      @article.contributors << Contributor.find(params[:contributors])
if params[:contributors]
      if @article.save
        [...]

This saves everything in the form, except the selected contributor(s).
What's driving me crazy is that I'm using exactly the same in the edit
method and it works fine (the selected contributors get saved):

 def edit
    @article = Article.find(params[:id])
    @article.attributes = params[:article]
     [...]
    if request.post?
      @article.contributors.clear
      @article.contributors << Contributor.find(params[:contributors])
if params[:contributors]
      if @article.save
            [...]

Typo, where I basically stole this method, from uses the same technique.
I don't understand why it's not saving when I create a new record, but
then works when I do an edit. The form is the same obviously, and I've
examined the param variables submitted and the contributors[] array is
there with the values in it. It's just not getting << into
article.contributos or saved properly. I checked the models in typo to
see if there were any special overloads for this, but there don't seem
to be.

I'm pulling my hair out with this problem, I know I must be missing
something stupid. Thanks in advance to anyone who can help.
6283a59f2a604f29df2a4a68b320cc1d?d=identicon&s=25 Chris Johanesen (Guest)
on 2006-02-23 07:45
Damn word wrap. Those "if params[:contributors]" lines are actually at
the end of the lines above, not on a line by themselves.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-23 15:16
(Received via mailing list)
Chris Johanesen wrote:
>       @article.contributors.clear
>       @article.contributors << Contributor.find(params[:contributors])
> if params[:contributors]
>       if @article.save
>         [...]
>
> This saves everything in the form, except the selected contributor(s).
> What's driving me crazy is that I'm using exactly the same in the edit
> method and it works fine (the selected contributors get saved):

This is a known bug in ActiveRecord in Rails 1.0.  Either move
to Edge Rails or save @article before adding contributors.

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.