On Friday, May 2, 2014 7:42:35 AM UTC-7, Jef Harkay wrote:
>
> I don't think I'm understanding the NestedAttributes plugin fully.  What 
> I'm trying to do is this... I have three tables:
>
>    - author
>    - book
>    - author_book (join table that maps authors to books, and vice 
>    versa... many to many)
>
> My Author model looks like this:
>
> class Author < Sequel::Model(:author)
>   many_to_many :book, left_key: :author_id, right_key: :book_id, :join_table 
> => :author_book
>   nested_attributes :bookend
>
> Now, I can create an Author just fine, but the problem then comes with 
> creating an Author with associated Books.  I'm trying to do something like 
> this:
>
> author = Author.new(:name => "blah", :book_attributes => [{:id => 1}, {:id => 
> 2}])
> author.save
>
> Unfortunately, this does not work, as I get the error 
> *nested_attributes_setter': 
> no matching associated object with given primary key (association: book, 
> pk: ["1"])*.  I'm assuming this is because this Author has not been 
> created yet, and so it doesn't have a primary key to insert into the join 
> table?  However, I can do something like this without error, but I'd rather 
> not...
>
> author = Author.new(:name => "blah")
> author.save
> author.add_book(Book[1])
> author.add_book(Book[2])
>
>
> So what am I not understanding here?  Can I accomplish what I'm wanting to 
> do or not?
>
If you just want to change which objects are associated, you probably want 
the association_pks plugin.  The nested_attributes plugin is for 
creating/updating associated objects at the same time as the current 
object, not for associating already existing objects to the current object.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to