Christos Zisopoulos
2006-Dec-20 02:26 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2006-Dec-20 03:20 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2006-Dec-20 14:25 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2006-Dec-20 14:54 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2006-Dec-20 14:59 UTC
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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-20 15:37 UTC
Re: has_many :through does it support collection_singular_ids ?
On Dec 20, 9:59 am, "Christos Zisopoulos" <chris...-zTsVz9fE6/I55T7KX7PIFA@public.gmane.org> wrote:> 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? > > -christosAlso, 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 :) Best, -r -- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike Weber
2006-Dec-20 16:37 UTC
Re: has_many :through does it support collection_singular_ids ?
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. On 12/20/06, ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > > On Dec 20, 9:59 am, "Christos Zisopoulos" < chris...-zTsVz9fE6/I55T7KX7PIFA@public.gmane.org> > wrote: > > 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 > :) > > Best, > > -r > > -- > Ryan Raaum > http://raaum.org > http://rails.raaum.org -- Rails docs > http://locomotive.raaum.org -- Self contained Rails for Mac OS X > > > > >-- Mike Weber Web Developer UW-Eau Claire --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2006-Dec-21 13:47 UTC
Re: has_many :through does it support collection_singular_ids ?
On 20 Dec 2006, at 16:37, ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> On Dec 20, 9:59 am, "Christos Zisopoulos" <chris...-zTsVz9fE6/I55T7KX7PIFA@public.gmane.org> > wrote: >> 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 > :)I think Mike Weber in his reply to my post confirms my suspicion; It doesn''t work :through. Cheers! -christos --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---