First of all i have a conceptual doubt, I have 2 tables, post and user and both have many to many relation. Ok i do put in both models Post Model has_and_belongs_to_many :users Users Model has_and_belongs_to_many :posts after this i''ved create posts_users model with id_user and id_post and a table for that, is this right??? Then another cuestion I want that when a post is created, generate a row in the posts_users table with the id_user and the id_post. If anyone can help please thank you very much. -- Felipe Vergara Contesse Ingeniería Civil Industrial UC --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Felipe Vergara wrote:> First of all i have a conceptual doubt, I have 2 tables, post and user > and > both have many to many relation. Ok i do put in both models > Post Model > has_and_belongs_to_many :users > Users Model > has_and_belongs_to_many :posts > > after this i''ved create posts_users model with id_user and id_post and > a > table for that, is this right???You don''t need the intermediate model if it has no further attributes. You just need a join table. create_table :users_posts, :id => false do |t| t.integer :user_id t.integer :post_id end You should probably also create indices and unique constraints to this table, but that''s fairly advanced.> Then another cuestion I want that when a post is created, generate a row > in > the posts_users table with the id_user and the id_post.This is automatically done for you if you set the model properties, e.g.: an_user.posts << a_post an_user.save> If anyone can help please thank you very much.Hope that helped, GG> -- > Felipe Vergara Contesse > Ingeniería Civil Industrial UC-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Aug 2008, at 00:51, Go Gormo wrote:> > Felipe Vergara wrote: >> First of all i have a conceptual doubt, I have 2 tables, post and >> user >> and >> both have many to many relation. Ok i do put in both models >> Post Model >> has_and_belongs_to_many :users >> Users Model >> has_and_belongs_to_many :posts >> >> after this i''ved create posts_users model with id_user and id_post >> and >> a >> table for that, is this right??? > > You don''t need the intermediate model if it has no further attributes. > You just need a join table. > > create_table :users_posts, :id => false do |t| > t.integer :user_id > t.integer :post_id > end >That does actually need to be posts_users (alphabetic order). You can of course override the name of the join table to be anything you want but there''s no reason to if you don''t need to. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thak you very much!!! that helped me a lot i have one last cuestion where do i put this?? an_user.posts << a_post an_user.save in the users model? On Thu, Aug 21, 2008 at 7:53 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 22 Aug 2008, at 00:51, Go Gormo wrote: > > > > > Felipe Vergara wrote: > >> First of all i have a conceptual doubt, I have 2 tables, post and > >> user > >> and > >> both have many to many relation. Ok i do put in both models > >> Post Model > >> has_and_belongs_to_many :users > >> Users Model > >> has_and_belongs_to_many :posts > >> > >> after this i''ved create posts_users model with id_user and id_post > >> and > >> a > >> table for that, is this right??? > > > > You don''t need the intermediate model if it has no further attributes. > > You just need a join table. > > > > create_table :users_posts, :id => false do |t| > > t.integer :user_id > > t.integer :post_id > > end > > > That does actually need to be posts_users (alphabetic order). You can > of course override the name of the join table to be anything you want > but there''s no reason to if you don''t need to. > > Fred > > > > > >-- Felipe Vergara Contesse Ingeniería Civil Industrial UC --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
in the controller (i guess) On Aug 22, 2:12 pm, "Felipe Vergara" <felverg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> thak you very much!!! that helped me a lot i have one last cuestion > > where do i put this?? > an_user.posts << a_post > an_user.save > in the users model? > On Thu, Aug 21, 2008 at 7:53 PM, Frederick Cheung < > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 22 Aug 2008, at 00:51, Go Gormo wrote: > > > > Felipe Vergara wrote: > > >> First of all i have a conceptual doubt, I have 2 tables, post and > > >> user > > >> and > > >> both have many to many relation. Ok i do put in both models > > >> Post Model > > >> has_and_belongs_to_many :users > > >> Users Model > > >> has_and_belongs_to_many :posts > > > >> after this i''ved create posts_users model with id_user and id_post > > >> and > > >> a > > >> table for that, is this right??? > > > > You don''t need the intermediate model if it has no further attributes. > > > You just need a join table. > > > > create_table :users_posts, :id => false do |t| > > > t.integer :user_id > > > t.integer :post_id > > > end > > > That does actually need to be posts_users (alphabetic order). You can > > of course override the name of the join table to be anything you want > > but there''s no reason to if you don''t need to. > > > Fred > > -- > Felipe Vergara Contesse > Ingeniería Civil Industrial UC--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---