Hello, good day! This is a rather intricate question coming from a not so experienced ruby on rails user. Let''s get to it: I am developing an app that has users, authors, microposts and tags. I want to implement something like a retweet button for the microposts: The task is simple: Get the params of a micropost(original user, original author, original tags) "Retweet" it to my own wall, using these params, but also with my own user_id embedded in it. So far I managed to "retweet" it, but there is one problem. As it is now, I am copying all the params and creating a new micropost with these params. As this creates exact duplicate, I would like to know what would be the best approach to implement this function without creating replicas all over my app. Here''s the codes: ROUTES.rb resources :microposts do member do get :retweet end end MICROPOSTS_CONTROLLER.rb def retweet original_micropost = Micropost.find(params[:id]) if original_micropost new_micropost = current_user.microposts.build(content: original_micropost.content, author_id: original_micropost.author_id) if new_micropost.save redirect_to user_path(current_user) flash[:success] = "Retweet Successful" else redirect_to user_path(current_user), notice: new_micropost.errors.full_messages end else redirect_back_or current_user flash[:error] = "Retweet error!" end end _MICROPOST.html.erb <%= link_to (image_tag "retweet.png"), reaspas_micropost_path(micropost) %> Let me know if there is anything else needed to get around this. Thank you in advance. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/X1Oo7Pk4Es8J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hello, good day! This is a rather intricate question coming from a not so experienced ruby on rails user. Let''s get to it: I am developing an app that has users, authors, microposts and tags. I want to implement something like a retweet button for the microposts: The task is simple: Get the params of a micropost(original user, original author, original tags) "Retweet" it to my own wall, using these params, but also with my own user_id embedded in it. So far I managed to "retweet" it, but there is one problem. As it is now, I am copying all the params and creating a new micropost with these params. As this creates exact duplicate, I would like to know what would be the best approach to implement this function without creating replicas all over my app. Here''s the codes: ROUTES.rb resources :microposts do member do get :retweet end end MICROPOSTS_CONTROLLER.rb def retweet original_micropost = Micropost.find(params[:id]) if original_micropost new_micropost = current_user.microposts.build(content: original_micropost.content, author_id: original_micropost.author_id) if new_micropost.save redirect_to user_path(current_user) flash[:success] = "Retweet Successful" else redirect_to user_path(current_user), notice: new_micropost.errors.full_messages end else redirect_back_or current_user flash[:error] = "Retweet error!" end end _MICROPOST.html.erb <%= link_to (image_tag "retweet.png"), retweet_micropost_path(micropost) %> Let me know if there is anything else needed to get around this. Thank you in advance. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/xuCjmisCmLkJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 April 2012 13:35, Marcos Korody <mfkorody-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, good day! > > This is a rather intricate question coming from a not so experienced ruby on > rails user. Let''s get to it: > > I am developing an app that has users, authors, microposts and tags. I want > to implement something like a retweet button for the microposts: > > The task is simple: > Get the params of a micropost(original user, original author, original tags) > "Retweet" it to my own wall, using these params, but also with my own > user_id embedded in it. > > So far I managed to "retweet" it, but there is one problem. As it is now, I > am copying all the params and creating a new micropost with these params. > As this creates exact duplicate, I would like to know what would be the best > approach to implement this function without creating replicas all over my > app.Think about the objects and relationships. You want a user to have many retweets (which are microposts) and a micropost must not only have its original owner but also users who have retweeted it. You have not told us the relationships at the moment but I assume that it is something like user has_many microposts and micropost belongs_to user. This does not quite match what you have as you have author_id but presumably you can sort that bit out. I would suggest using a join table possibly called retweetings where user has_many retweetings and user has_many retweets through retweetings. Also micropost has_many retweetings and has_many retweeters through retweetings. Then for retweetings you just need retweeter_id and retweet_id and have retweetings belongs_to :retweeter, :class_name => "user" and belongs_to :retweet, :class_name => "micropost" You probably need the class_name spec on the has_many through relationships too. Looking back at the post you seem to have authors and users in different tables. Are you sure that is right, what is the difference between an author and a user? Colin> > Here''s the codes: > > ROUTES.rb > > resources :microposts do > member do > get :retweet > end > end > > > MICROPOSTS_CONTROLLER.rb > > def retweet > original_micropost = Micropost.find(params[:id]) > if original_micropost > new_micropost = current_user.microposts.build(content: > original_micropost.content, author_id: original_micropost.author_id) > if new_micropost.save > redirect_to user_path(current_user) > flash[:success] = "Retweet Successful" > else > redirect_to user_path(current_user), notice: > new_micropost.errors.full_messages > end > else > redirect_back_or current_user > flash[:error] = "Retweet error!" > end > end > > > > _MICROPOST.html.erb > > <%= link_to (image_tag "retweet.png"), reaspas_micropost_path(micropost) %> > > > > Let me know if there is anything else needed to get around this. > > Thank you in advance. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/X1Oo7Pk4Es8J. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 17 April 2012 15:04, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 17 April 2012 13:35, Marcos Korody <mfkorody-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hello, good day! >> >> This is a rather intricate question coming from a not so experienced ruby on >> rails user. Let''s get to it: >> >> I am developing an app that has users, authors, microposts and tags. I want >> to implement something like a retweet button for the microposts: >> >> The task is simple: >> Get the params of a micropost(original user, original author, original tags) >> "Retweet" it to my own wall, using these params, but also with my own >> user_id embedded in it. >> >> So far I managed to "retweet" it, but there is one problem. As it is now, I >> am copying all the params and creating a new micropost with these params. >> As this creates exact duplicate, I would like to know what would be the best >> approach to implement this function without creating replicas all over my >> app. > > Think about the objects and relationships. You want a user to have > many retweets (which are microposts) and a micropost must not only > have its original owner but also users who have retweeted it. > > You have not told us the relationships at the moment but I assume that > it is something like user has_many microposts and micropost belongs_to > user. This does not quite match what you have as you have author_id > but presumably you can sort that bit out. > > I would suggest using a join table possibly called retweetings where > user has_many retweetings and user has_many retweets through > retweetings. Also micropost has_many retweetings and has_many > retweeters through retweetings. > > Then for retweetings you just need retweeter_id and retweet_id and > have retweetings belongs_to :retweeter, :class_name => "user" and > belongs_to :retweet, :class_name => "micropost" > > You probably need the class_name spec on the has_many through relationships too. > > Looking back at the post you seem to have authors and users in > different tables. Are you sure that is right, what is the difference > between an author and a user?I meant to say have a look at the Rails Guide on ActiveRecord Associations for some background data on this if you do not understand what I mean. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hello Colin, good day! Thank you for your prompt response. I am sorry if I failed to provide some basic information. Ur assumptions were right: USER has_many MICROPOSTS MICROPOST belongs_to USER The relations are exactly the same for author. My app has Users and Authors and they do live in two different tables. It is a Quotation app, so Authors are famous people and Users are normal people. Even though the tables are pretty much the same, they have security differences. Any given user could create an Author Profile which has attributes that are accessible to any other user to change. And of course, this does not happen in the user''s table. I understand the Join table approach. Will dig deeper through this way and will get back to you with more updates. Thanks again! Em terça-feira, 17 de abril de 2012 11h06min09s UTC-3, Colin Law escreveu:> > On 17 April 2012 15:04, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > On 17 April 2012 13:35, Marcos Korody <mfkorody-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hello, good day! > >> > >> This is a rather intricate question coming from a not so experienced > ruby on > >> rails user. Let''s get to it: > >> > >> I am developing an app that has users, authors, microposts and tags. I > want > >> to implement something like a retweet button for the microposts: > >> > >> The task is simple: > >> Get the params of a micropost(original user, original author, original > tags) > >> "Retweet" it to my own wall, using these params, but also with my own > >> user_id embedded in it. > >> > >> So far I managed to "retweet" it, but there is one problem. As it is > now, I > >> am copying all the params and creating a new micropost with these > params. > >> As this creates exact duplicate, I would like to know what would be the > best > >> approach to implement this function without creating replicas all over > my > >> app. > > > > Think about the objects and relationships. You want a user to have > > many retweets (which are microposts) and a micropost must not only > > have its original owner but also users who have retweeted it. > > > > You have not told us the relationships at the moment but I assume that > > it is something like user has_many microposts and micropost belongs_to > > user. This does not quite match what you have as you have author_id > > but presumably you can sort that bit out. > > > > I would suggest using a join table possibly called retweetings where > > user has_many retweetings and user has_many retweets through > > retweetings. Also micropost has_many retweetings and has_many > > retweeters through retweetings. > > > > Then for retweetings you just need retweeter_id and retweet_id and > > have retweetings belongs_to :retweeter, :class_name => "user" and > > belongs_to :retweet, :class_name => "micropost" > > > > You probably need the class_name spec on the has_many through > relationships too. > > > > Looking back at the post you seem to have authors and users in > > different tables. Are you sure that is right, what is the difference > > between an author and a user? > > I meant to say have a look at the Rails Guide on ActiveRecord > Associations for some background data on this if you do not understand > what I mean. > > Colin > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/CxxqCBnQr1AJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hey Collin! Good day! I managed to implement the functionality I was seeking through a join model. Thanks for your inputs! Em terça-feira, 17 de abril de 2012 11h04min45s UTC-3, Colin Law escreveu:> > On 17 April 2012 13:35, Marcos Korody <mfkorody-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello, good day! > > > > This is a rather intricate question coming from a not so experienced > ruby on > > rails user. Let''s get to it: > > > > I am developing an app that has users, authors, microposts and tags. I > want > > to implement something like a retweet button for the microposts: > > > > The task is simple: > > Get the params of a micropost(original user, original author, original > tags) > > "Retweet" it to my own wall, using these params, but also with my own > > user_id embedded in it. > > > > So far I managed to "retweet" it, but there is one problem. As it is > now, I > > am copying all the params and creating a new micropost with these params. > > As this creates exact duplicate, I would like to know what would be the > best > > approach to implement this function without creating replicas all over my > > app. > > Think about the objects and relationships. You want a user to have > many retweets (which are microposts) and a micropost must not only > have its original owner but also users who have retweeted it. > > You have not told us the relationships at the moment but I assume that > it is something like user has_many microposts and micropost belongs_to > user. This does not quite match what you have as you have author_id > but presumably you can sort that bit out. > > I would suggest using a join table possibly called retweetings where > user has_many retweetings and user has_many retweets through > retweetings. Also micropost has_many retweetings and has_many > retweeters through retweetings. > > Then for retweetings you just need retweeter_id and retweet_id and > have retweetings belongs_to :retweeter, :class_name => "user" and > belongs_to :retweet, :class_name => "micropost" > > You probably need the class_name spec on the has_many through > relationships too. > > Looking back at the post you seem to have authors and users in > different tables. Are you sure that is right, what is the difference > between an author and a user? > > Colin > > > > > Here''s the codes: > > > > ROUTES.rb > > > > resources :microposts do > > member do > > get :retweet > > end > > end > > > > > > MICROPOSTS_CONTROLLER.rb > > > > def retweet > > original_micropost = Micropost.find(params[:id]) > > if original_micropost > > new_micropost = current_user.microposts.build(content: > > original_micropost.content, author_id: original_micropost.author_id) > > if new_micropost.save > > redirect_to user_path(current_user) > > flash[:success] = "Retweet Successful" > > else > > redirect_to user_path(current_user), notice: > > new_micropost.errors.full_messages > > end > > else > > redirect_back_or current_user > > flash[:error] = "Retweet error!" > > end > > end > > > > > > > > _MICROPOST.html.erb > > > > <%= link_to (image_tag "retweet.png"), reaspas_micropost_path(micropost) > %> > > > > > > > > Let me know if there is anything else needed to get around this. > > > > Thank you in advance. > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To view this discussion on the web visit > > https://groups.google.com/d/msg/rubyonrails-talk/-/X1Oo7Pk4Es8J. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/19u5WY_OzYAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hey Marcos, I was wondering if you could share the code you added for the retweet function. I am looking to do the exact same, but am a bit stuck. thank you On Thursday, 19 April 2012 02:13:58 UTC+1, Marcos Korody wrote:> > Hey Collin! Good day! > > I managed to implement the functionality I was seeking through a join > model. > > Thanks for your inputs! > > Em terça-feira, 17 de abril de 2012 11h04min45s UTC-3, Colin Law escreveu: >> >> On 17 April 2012 13:35, Marcos Korody <mfko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> >> wrote: >> > Hello, good day! >> > >> > This is a rather intricate question coming from a not so experienced >> ruby on >> > rails user. Let''s get to it: >> > >> > I am developing an app that has users, authors, microposts and tags. I >> want >> > to implement something like a retweet button for the microposts: >> > >> > The task is simple: >> > Get the params of a micropost(original user, original author, original >> tags) >> > "Retweet" it to my own wall, using these params, but also with my own >> > user_id embedded in it. >> > >> > So far I managed to "retweet" it, but there is one problem. As it is >> now, I >> > am copying all the params and creating a new micropost with these >> params. >> > As this creates exact duplicate, I would like to know what would be the >> best >> > approach to implement this function without creating replicas all over >> my >> > app. >> >> Think about the objects and relationships. You want a user to have >> many retweets (which are microposts) and a micropost must not only >> have its original owner but also users who have retweeted it. >> >> You have not told us the relationships at the moment but I assume that >> it is something like user has_many microposts and micropost belongs_to >> user. This does not quite match what you have as you have author_id >> but presumably you can sort that bit out. >> >> I would suggest using a join table possibly called retweetings where >> user has_many retweetings and user has_many retweets through >> retweetings. Also micropost has_many retweetings and has_many >> retweeters through retweetings. >> >> Then for retweetings you just need retweeter_id and retweet_id and >> have retweetings belongs_to :retweeter, :class_name => "user" and >> belongs_to :retweet, :class_name => "micropost" >> >> You probably need the class_name spec on the has_many through >> relationships too. >> >> Looking back at the post you seem to have authors and users in >> different tables. Are you sure that is right, what is the difference >> between an author and a user? >> >> Colin >> >> > >> > Here''s the codes: >> > >> > ROUTES.rb >> > >> > resources :microposts do >> > member do >> > get :retweet >> > end >> > end >> > >> > >> > MICROPOSTS_CONTROLLER.rb >> > >> > def retweet >> > original_micropost = Micropost.find(params[:id]) >> > if original_micropost >> > new_micropost = current_user.microposts.build(content: >> > original_micropost.content, author_id: original_micropost.author_id) >> > if new_micropost.save >> > redirect_to user_path(current_user) >> > flash[:success] = "Retweet Successful" >> > else >> > redirect_to user_path(current_user), notice: >> > new_micropost.errors.full_messages >> > end >> > else >> > redirect_back_or current_user >> > flash[:error] = "Retweet error!" >> > end >> > end >> > >> > >> > >> > _MICROPOST.html.erb >> > >> > <%= link_to (image_tag "retweet.png"), >> reaspas_micropost_path(micropost) %> >> > >> > >> > >> > Let me know if there is anything else needed to get around this. >> > >> > Thank you in advance. >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups >> > "Ruby on Rails: Talk" group. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msg/rubyonrails-talk/-/X1Oo7Pk4Es8J. >> > To post to this group, send email to rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> >> . >> > To unsubscribe from this group, send email to >> > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. >> > For more options, visit this group at >> > http://groups.google.com/group/rubyonrails-talk?hl=en. >> >>-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Ft6-PuSpTz0J. For more options, visit https://groups.google.com/groups/opt_out.