On Wed, Feb 15, 2012 at 11:55 AM, Bodo Kist <li...@ruby-forum.com> wrote:

> Thanks.
>
> I forgot to mention that one solution is discussed on
>
>
> http://stackoverflow.com/questions/9174513/rails-has-many-through-form-with-checkboxes-and-extra-field-in-the-join-model/9191867#9191867
>
> When I try this approach I got an error message from inside the original
> routine below:
>
> Couldn't find Questionnaire without an ID
>
> (In my case Product is User, Category is Form and Categorization is
> Questionnaire.)
>
> def initialized_categorizations # this is the key method
>    [].tap do |o|
>      Category.all.each do |category|
>        if c = categorizations.find { |c| c.category_id == category.id }
>          o << c.tap { |c| c.enable ||= true }
>        else
>          o << Categorization.new(category: category)
>        end
>      end
>    end
>  end
> ...
>
> My question is what the error line tries to do in detail.
>

In that case, could you then send your particular implementation
and the stack trace that you got.

A wild guess is that there is a problem where looking for
an ID (that is only generated when saving to the database;
but at this time your questionaire object is still in the making
in memory and does not have an id yet).

That is why I used the "select" code in my proposal (checks
in the array in memory using the belongs_to result == self)
and not find (which would look for an entry in db, but we have
not saved yet at that point).

HTH,

Peter

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to