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? -- 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 Sep 3, 2007, at 10:03 AM, Max Williams wrote:> > 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? > -- > 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 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn 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! -- 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 -~----------~----~----~----~------~----~------~--~---
Max Williams wrote:> Rob Biedenharn 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 ] -- 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 -~----------~----~----~----~------~----~------~--~---
You can use Max Williams suggestion if you always want the condition with the SQL or you could do as you did with defining a function for it! :) -- 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 -~----------~----~----~----~------~----~------~--~---
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 Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 3, 2007, at 10:03 AM, Max Williams wrote: > > > > > > > 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? > > -- > > Posted viahttp://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 Biedenharn http://agileconsultingllc.com > R...-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 -~----------~----~----~----~------~----~------~--~---
> > 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! -- 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 -~----------~----~----~----~------~----~------~--~---