Problems select correct records from 2 tables

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 ?

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 T. 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 ?

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 T. [email protected]
wrote:

def show
@news = News.find(params[:id])
end

And then in your view, eg.:

<% @news.comments.each do |comment| %>

<%= comment.message %>


<% end %>

On Mar 22, 1:13 pm, Mister T. [email protected]
wrote:

def show
@news = News.find(params[:id])
end

And then in your view, eg.:

<% @news.comments.each do |comment| %>

<%= comment.message %>


<% end %>

On Mar 22, 2007, at 9:07 AM, Mister T. 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:

    <% for comment in @news.approved_comments -%>
  • <%= comment.body %>
  • <% end -%>

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 B. http://agileconsultingllc.com
[email protected]

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| %>

<%= comment.message %>


<% end %>

Br,

Morten

On Mar 22, 2:07 pm, Mister T. [email protected]

Thanks Rob and Morten!
I’ve used Rob’s implementation as I read that one first :slight_smile:
It works like a charm.