Forum: Ruby on Rails Problems select correct records from 2 tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf6fd030015a87035997fd19374e7746?d=identicon&s=25 Mister Twister (webdevotion)
on 2007-03-22 12:40
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 ?
Cf6fd030015a87035997fd19374e7746?d=identicon&s=25 Mister Twister (webdevotion)
on 2007-03-22 13:13
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 ?
13e4ba7f654dbbdd186d702c06c8b24c?d=identicon&s=25 Morten (Guest)
on 2007-03-22 13:59
(Received via mailing list)
On Mar 22, 1:13 pm, Mister Twister <rails-mailing-l...@andreas-s.net>
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 %>
Cf6fd030015a87035997fd19374e7746?d=identicon&s=25 Mister Twister (webdevotion)
on 2007-03-22 14:07
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...@andreas-s.net>
> 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 %>
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-03-22 15:01
(Received via mailing list)
On Mar 22, 2007, at 9:07 AM, Mister Twister wrote:
>> def show
>> <% 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@AgileConsultingLLC.com
13e4ba7f654dbbdd186d702c06c8b24c?d=identicon&s=25 Morten (Guest)
on 2007-03-22 15:39
(Received via mailing list)
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...@andreas-s.net>
Cf6fd030015a87035997fd19374e7746?d=identicon&s=25 Mister Twister (webdevotion)
on 2007-03-22 17:06
Thanks Rob and Morten!
I've used Rob's implementation as I read that one first :)
It works like a charm.
This topic is locked and can not be replied to.