Hello, I have 2 tables: news and comments. news has many comments and comment belongs to article On the show action I want to show all the comments that belong to a news item, excluding the ones that are not approved yet. It''s the excluding part that''s causing me problems. example sql (from the head, not tested): select news.id,news.title,news.article,comments.message, ... from news,comments where comments.news_id = news.id AND approved = 1 How do I set this up in the models ? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Is this the correct solution to my problem ? Or is there a better way? in my news controller: def show @news = News.find( params[:id] ) @comments = Comment.find(:all,:conditions=>"news_id = " + params[:id].to_s ) end Mister Twister wrote:> Hello, > > I have 2 tables: news and comments. > > news has many comments and comment belongs to article > > On the show action I want to show all the comments that > belong to a news item, excluding the ones that are not approved yet. > It''s the excluding part that''s causing me problems. > > example sql (from the head, not tested): > > select news.id,news.title,news.article,comments.message, ... > from news,comments > where comments.news_id = news.id > AND approved = 1 > > How do I set this up in the models ?-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 22, 1:13 pm, Mister Twister <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Is this the correct solution to my problem ? > Or is there a better way? > > in my news controller: > > def show > @news = News.find( params[:id] ) > @comments = Comment.find(:all,:conditions=>"news_id = " + > params[:id].to_s ) > end >def show @news = News.find(params[:id]) end And then in your view, eg.: <% @news.comments.each do |comment| %> <p> <%= comment.message %> </p> <hr/> <% end %> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Morten, But this does not get rid of the comments that are not approved yet. It shows all the comments. I only want to show the comments that are approved.> On Mar 22, 1:13 pm, Mister Twister <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> > def show > @news = News.find(params[:id]) > end > > And then in your view, eg.: > > <% @news.comments.each do |comment| %> > <p> > <%= comment.message %> > </p> > <hr/> > <% end %>-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 22, 2007, at 9:07 AM, Mister Twister wrote:> Thanks Morten, > > But this does not get rid of the comments that are not approved yet. > It shows all the comments. > > I only want to show the comments that are approved. > >> On Mar 22, 1:13 pm, Mister Twister <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> >> def show >> @news = News.find(params[:id]) >> end >> >> And then in your view, eg.: >> >> <% @news.comments.each do |comment| %> >> <p> >> <%= comment.message %> >> </p> >> <hr/> >> <% end %>Assuming that your comments.approved column is a boolean, you can do this: class News < ActiveRecord::Base has_many :approved_comments, :class_name => ''Comment'', :conditions => [''approved = ?'', true] end class Comment < ActiveRecord::Base belongs_to :news end Doing it this way should be correct for any database (even when boolean is implemented by 0/1). Then in your controller: def show @news = News.find( params[:id] ) @comments = @news.approved_comments # or just do this in your view end And your view: <ul> <% for comment in @news.approved_comments -%> <li><%= comment.body %></li> <% end -%> </ul> Of course, your markup is whatever you prefer and I''ve guessed that the interesting piece from a comment is in a "body" column, but this should help you out. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I see. Then I would do this (you may need to change the condition): class News has_many :comments do def approved @approved_comments ||= find(:all, :conditions => ''is_approved IS TRUE'') end end #The rest of the class News... end And in your view: <% @news.comments.approved.each do |comment| %> <p> <%= comment.message %> </p> <hr/> <% end %> Br, Morten On Mar 22, 2:07 pm, Mister Twister <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks Morten, > > But this does not get rid of the comments that are not approved yet. > It shows all the comments. > > I only want to show the comments that are approved. > > > On Mar 22, 1:13 pm, Mister Twister <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > def show > > @news = News.find(params[:id]) > > end > > > And then in your view, eg.: > > > <% @news.comments.each do |comment| %> > > <p> > > <%= comment.message %> > > </p> > > <hr/> > > <% end %> > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Rob and Morten! I''ve used Rob''s implementation as I read that one first :) It works like a charm. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---