Simple select question

Currently i’m getting all the stories that belong to a given article,
and listing them, with

for story in @article.story

the usual sort of thing, right?

I want to add a condition, that only selects stories where the
‘complete’ field is equal to 1. So, it’s like saying

for story in Story.find(:all, :conditions => [“article_id = ? and
complete = ?”, @article, 1])

but in the view. There must be a simple way to do this but i can’t find
it - can someone help please?

On Sep 3, 2007, at 10:03 AM, Max W. wrote:

for story in Story.find(:all, :conditions => [“article_id = ? and
complete = ?”, @article, 1])

but in the view. There must be a simple way to do this but i can’t
find
it - can someone help please?

Posted via http://www.ruby-forum.com/.

in Model:

class Article
has_many :completed_stories, :conditions => [ ‘complete = ?’, true ]
end

(If you’re declaring the complete column to be a boolean, then this
is better than using 1 even though the SQL will be exactly the same
as it reveals the meaning more.)

in view:

for story in @article.completed_stories

Alternatively, you could set a @completed_stories instance variable
in the controller and just have “for story in @completed_stories” in
the view.

You’ll find that the system is more easily changed later if you push
this kind of logic into the model wherever practical.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

in Model:

class Article
has_many :completed_stories, :conditions => [ ‘complete = ?’, true ]
end

Ah - that’s great thanks. I ended up doing something similar, making a
method in the article model:

def completed_stories
Story.find(:all, :conditions => [“article_id = ? and complete = ?”,
self, 1])
end

but your suggested way is much nicer. Cheers!

Max W. wrote:

Rob B. wrote:

in Model:

class Article
has_many :completed_stories, :conditions => [ ‘complete = ?’, true ]
end

Ah - that’s great thanks. I ended up doing something similar, making a
method in the article model:

def completed_stories
Story.find(:all, :conditions => [“article_id = ? and complete = ?”,
self, 1])
end

but your suggested way is much nicer. Cheers!

Actually there’s an amendment needed (in my case at least), in case
anyone else is trying to do this: you need to specify the class that
:completed_stories is coming from:

has_many :completed_stories,
:class_name => “Story”,
:conditions => [ ‘complete = ?’, true ]

You can use Max W. suggestion if you always want the condition
with the SQL or you could do as you did with defining a function for it!
:slight_smile:

There are a couple ways to do it that I would suggest.

Adding another association works but it’s a bit cumbersome. Luckily,
rails provides these lovely association extensions you can use to
define new methods on the association:

class Article < ActiveRecord::Base

has_many :stories do
def completed
find(:all, :conditions => “complete = 1”)
end
end

This allows you to do:

@article.stories.completed

rather than @article.completed_stories

Alternatively, once you’re in the view, you can always use select to
filter the array:

@article.stories.select{ |story| story.complete == 1}.each do |
story| ;; end

Not the cleanest, but if you’re already using @article.stories
elsewhere, it won’t hit the database again.

Rein

On Sep 3, 11:19 am, Rob B. [email protected]

class Article < ActiveRecord::Base

has_many :stories do
def completed
find(:all, :conditions => “complete = 1”)
end
end

Alternatively, once you’re in the view, you can always use select to
filter the array:

@article.stories.select{ |story| story.complete == 1}.each do |
story| ;; end

Not the cleanest, but if you’re already using @article.stories
elsewhere, it won’t hit the database again.

Both good tips, thanks Rein!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs