has_many :through does it support collection_singular_ids ?

Good evening list members,

I have the following model:

class Author < ActiveRecord::Base
   has_many :authorships
   has_many :books, :through => : authorships
end

Should I be able to do the following?

Author.find(:first).book_ids = [1,2,3]

I ask, because it doesn't. The docs state that it should, but it
doesn't specialise in the case of a :through

This works fine BTW:

Author.find(:first).authorship_ids = [12,13]

Do I really need to find and add (<<) my associated join models for
books one by one?

-christos

Good evening list members,

I have the following model:

class Author < ActiveRecord::Base
   has_many :authorships
   has_many :books, :through => : authorships
end

Should I be able to do the following?

Author.find(:first).book_ids = [1,2,3]

Hi Christos,

What are you trying to do here? It looks to me as if you are trying to
add some books to an author... if so, how about:

a = Author.find(:first)
Book.find(1,2,3).each { |b| Authorship.create(:author=>a, :book=>b) }

or, in Rails 1.2

a = Author.find(:first)
Book.find(1,2,3).each { |b| a.books << b }

I ask, because it doesn't. The docs state that it should, but it
doesn't specialise in the case of a :through

This works fine BTW:

Author.find(:first).authorship_ids = [12,13]

Do I really need to find and add (<<) my associated join models for
books one by one?

-christos

Also, I would almost never expect to directly use foreign keys in
rails, so you should begin to wonder if you find yourself doing so...

(If I've totally missed the point here, let me know and I'll try again
:slight_smile:

Best,

-r

As I understand it handing in an array of ids to book_ids only works for a has_many relationship. Even though it’s declared as a has_many relationship, has_many :through is technically a has_and_belongs_to_many relationship.
I have not tested this thoroughly, it’s just how I believe it works.

Good evening list members,

I have the following model:

class Author < ActiveRecord::Base
   has_many :authorships
   has_many :books, :through => : authorships
end

Should I be able to do the following?

Author.find(:first).book_ids = [1,2,3]

Hi Christos,

What are you trying to do here? It looks to me as if you are trying to
add some books to an author... if so, how about:

[snip]

Also, I would almost never expect to directly use foreign keys in
rails, so you should begin to wonder if you find yourself doing so...

I am trying to assign a bunch of books returned from something like this in my view:

  <% Book.find(:all).each do |team| %>
   <%= check_box_tag "book_ids[]", book.id %> <%= book.title %><br />
  <% end %>

(I could have used a collection_select, but the above is an example only)

a = Author.find(:first)
Book.find(1,2,3).each { |b| a.books << b }

This is what I ended up doing...

(If I've totally missed the point here, let me know and I'll try again
:slight_smile:

I think Mike Weber in his reply to my post confirms my suspicion; It doesn't work :through.

Cheers!
-christos