# Simple select question

**URL:** <https://www.ruby-forum.com/t/simple-select-question/113659>\
**Category:** Rails\
**Created:** [September 3, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659 "2007-09-03T16:03:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/1 "2007-09-03T16:03:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/2 "2007-09-03T18:23:09Z")

</div>

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/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/3 "2007-09-03T18:41:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/4 "2007-09-03T18:46:53Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/5 "2007-09-03T19:55:05Z")

</div>

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!  
🙂

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/6 "2007-09-03T20:10:36Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 3, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/simple-select-question/113659/7 "2007-09-03T20:30:03Z")

</div>

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