Hello, I have 3 models: class Result < ActiveRecord::Base has_many :collections has_many :articles, :through => :collections end class Article < ActiveRecord::Base has_many :collections has_many :results, :through => :collections end class Collection < ActiveRecord::Base belongs_to :result belongs_to :article end Collection has a table like this: create_table :collections do |t| t.column :id, :integer t.column :result_id, :integer t.column :article_id, :integer t.column :extract, :text end The problem i have is this. I do a find for a single result. Then i go through each article in this result, using the each method. At this point i''m outputting all the attributes of articles. But i want to also output the extract for this article and result. How would i do this? article.extract doesn''t work, nor does article.results.extract Thanks for your help. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
In your design article has many collections therefor article also has many extracts. Which means that article will not have an extract method which is why article.extract will not work. In the same way article.results method will give you an Array of Result object so obviously will not have an extract method. This is why articles.results.extract will not work. Maybe that will help you rethink your design to produce your desired results. On Feb 20, 5:36 pm, "Mooktakim Ahmed" <m...-lNzBllt/ka8ybS5Ee8rs3A@public.gmane.org> wrote:> Hello, > > I have 3 models: > > class Result < ActiveRecord::Base > has_many :collections > has_many :articles, :through => :collections > end > > class Article < ActiveRecord::Base > has_many :collections > has_many :results, :through => :collections > end > > class Collection < ActiveRecord::Base > belongs_to :result > belongs_to :article > end > > Collection has a table like this: > create_table :collections do |t| > t.column :id, :integer > t.column :result_id, :integer > t.column :article_id, :integer > t.column :extract, :text > end > > The problem i have is this. > I do a find for a single result. > Then i go through each article in this result, using the each method. > > At this point i''m outputting all the attributes of articles. > But i want to also output the extract for this article and result. > > How would i do this? > > article.extract doesn''t work, nor does article.results.extract > > Thanks for your help.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oops slight typo in previous post articles.results.extract should have been article.results.extract On Feb 20, 5:50 pm, "Robert Walker" <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In your design article has many collections therefor article also has > many extracts. Which means that article will not have an extract > method which is why article.extract will not work. > > In the same way article.results method will give you an Array of > Result object so obviously will not have an extract method. This is > why articles.results.extract will not work. > > Maybe that will help you rethink your design to produce your desired > results. > > On Feb 20, 5:36 pm, "Mooktakim Ahmed" <m...-lNzBllt/ka8ybS5Ee8rs3A@public.gmane.org> wrote: > > > Hello, > > > I have 3 models: > > > class Result < ActiveRecord::Base > > has_many :collections > > has_many :articles, :through => :collections > > end > > > class Article < ActiveRecord::Base > > has_many :collections > > has_many :results, :through => :collections > > end > > > class Collection < ActiveRecord::Base > > belongs_to :result > > belongs_to :article > > end > > > Collection has a table like this: > > create_table :collections do |t| > > t.column :id, :integer > > t.column :result_id, :integer > > t.column :article_id, :integer > > t.column :extract, :text > > end > > > The problem i have is this. > > I do a find for a single result. > > Then i go through each article in this result, using the each method. > > > At this point i''m outputting all the attributes of articles. > > But i want to also output the extract for this article and result. > > > How would i do this? > > > article.extract doesn''t work, nor does article.results.extract > > > Thanks for your help.--~--~---------~--~----~------------~-------~--~----~ 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''ll need to find the collection and use collection.extract. Easiest might be something like.. result.articles.each do |article| collection = article.collections.find(:first, :conditions => ["result_id = ?", result.id]) end This assumes that there''s only ever one collection associated with any article/result pair. Mooktakim Ahmed wrote:> Hello, > > I have 3 models: > > class Result < ActiveRecord::Base > has_many :collections > has_many :articles, :through => :collections > end > > class Article < ActiveRecord::Base > has_many :collections > has_many :results, :through => :collections > end > > class Collection < ActiveRecord::Base > belongs_to :result > belongs_to :article > end > > Collection has a table like this: > create_table :collections do |t| > t.column :id, :integer > t.column :result_id, :integer > t.column :article_id, :integer > t.column :extract, :text > end > > > The problem i have is this. > I do a find for a single result. > Then i go through each article in this result, using the each method. > > At this point i''m outputting all the attributes of articles. > But i want to also output the extract for this article and result. > > How would i do this? > > article.extract doesn''t work, nor does article.results.extract > > Thanks for your help. > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks. But, won''t this run SQL query for each article in result? There must be an easy way to combine both article and result, using collection. I''ll use this for now, until i find something better. On Tue, February 20, 2007 10:58 pm, Jon Garvin wrote:>> You''ll need to find the collection and use collection.extract. Easiest > might be something like.. > > result.articles.each do |article| collection = article.collections.find(:first, > :conditions => > ["result_id = ?", result.id]) > end > > This assumes that there''s only ever one collection associated with any > article/result pair. > > Mooktakim Ahmed wrote: > >> Hello, >> >> >> I have 3 models: >> >> >> class Result < ActiveRecord::Base has_many :collections has_many :articles, :through => >> :collections >> end >> >> class Article < ActiveRecord::Base has_many :collections has_many :results, :through => >> :collections >> end >> >> class Collection < ActiveRecord::Base belongs_to :result belongs_to :article end >> >> Collection has a table like this: >> create_table :collections do |t| t.column :id, :integer t.column :result_id, :integer >> t.column :article_id, :integer t.column :extract, :text end >> >> >> The problem i have is this. >> I do a find for a single result. >> Then i go through each article in this result, using the each method. >> >> >> At this point i''m outputting all the attributes of articles. >> But i want to also output the extract for this article and result. >> >> >> How would i do this? >> >> >> article.extract doesn''t work, nor does article.results.extract >> >> Thanks for your help. >> >> >> >>> >> >> > > > > > >-- Website: http://www.mooktakim.com email: mma-lNzBllt/ka8ybS5Ee8rs3A@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 -~----------~----~----~----~------~----~------~--~---
I understand. I thought because i''ve combined both article and result using collection, the extract attribute might be included in both. There must be a nice and simple way to do this, without going through every article, or result. Anyway, thanks for your help. On Tue, February 20, 2007 10:53 pm, Robert Walker wrote:>> Oops slight typo in previous post > > > articles.results.extract > > should have been > > article.results.extract > > > On Feb 20, 5:50 pm, "Robert Walker" <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> In your design article has many collections therefor article also has >> many extracts. Which means that article will not have an extract method which is why >> article.extract will not work. >> >> In the same way article.results method will give you an Array of >> Result object so obviously will not have an extract method. This is >> why articles.results.extract will not work. >> >> Maybe that will help you rethink your design to produce your desired >> results. >> >> On Feb 20, 5:36 pm, "Mooktakim Ahmed" <m...-lNzBllt/ka8ybS5Ee8rs3A@public.gmane.org> wrote: >> >> >>> Hello, >>> >> >>> I have 3 models: >>> >> >>> class Result < ActiveRecord::Base has_many :collections has_many :articles, :through >>> => :collections >>> end >> >>> class Article < ActiveRecord::Base has_many :collections has_many :results, :through >>> => :collections >>> end >> >>> class Collection < ActiveRecord::Base belongs_to :result belongs_to :article end >> >>> Collection has a table like this: >>> create_table :collections do |t| t.column :id, :integer t.column :result_id, :integer >>> t.column :article_id, :integer t.column :extract, :text end >> >>> The problem i have is this. >>> I do a find for a single result. >>> Then i go through each article in this result, using the each method. >>> >> >>> At this point i''m outputting all the attributes of articles. >>> But i want to also output the extract for this article and result. >>> >> >>> How would i do this? >>> >> >>> article.extract doesn''t work, nor does article.results.extract >> >>> Thanks for your help. >>> > > > > > >-- Website: http://www.mooktakim.com email: mma-lNzBllt/ka8ybS5Ee8rs3A@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 -~----------~----~----~----~------~----~------~--~---
use eager loading. That will only take one SQL query, and you will have all the stuff you need in your result. (code not tested and probably contains typos ;) ) #controller @result = Result.find :first, :conditions => [someting], :include => {:collections => :articles } #view <%= @result.collections.each do |coll| %> .... access the extract... <%= coll.extract %> ....access the article attributes with:.... <%= coll.article.some_attribute %> end On 21 Feb., 00:29, "Mooktakim Ahmed" <m...-lNzBllt/ka8ybS5Ee8rs3A@public.gmane.org> wrote:> I understand. I thought because i''ve combined both article and result using collection, > the extract attribute might be included in both. > > There must be a nice and simple way to do this, without going through every article, or > result. > > Anyway, thanks for your help. > > > > > > On Tue, February 20, 2007 10:53 pm, Robert Walker wrote: > > > Oops slight typo in previous post > > > articles.results.extract > > > should have been > > > article.results.extract > > > On Feb 20, 5:50 pm, "Robert Walker" <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> In your design article has many collections therefor article also has > >> many extracts. Which means that article will not have an extract method which is why > >> article.extract will not work. > > >> In the same way article.results method will give you an Array of > >> Result object so obviously will not have an extract method. This is > >> why articles.results.extract will not work. > > >> Maybe that will help you rethink your design to produce your desired > >> results. > > >> On Feb 20, 5:36 pm, "Mooktakim Ahmed" <m...-lNzBllt/ka8ybS5Ee8rs3A@public.gmane.org> wrote: > > >>> Hello, > > >>> I have 3 models: > > >>> class Result < ActiveRecord::Base has_many :collections has_many :articles, :through > >>> => :collections > >>> end > > >>> class Article < ActiveRecord::Base has_many :collections has_many :results, :through > >>> => :collections > >>> end > > >>> class Collection < ActiveRecord::Base belongs_to :result belongs_to :article end > > >>> Collection has a table like this: > >>> create_table :collections do |t| t.column :id, :integer t.column :result_id, :integer > >>> t.column :article_id, :integer t.column :extract, :text end > > >>> The problem i have is this. > >>> I do a find for a single result. > >>> Then i go through each article in this result, using the each method. > > >>> At this point i''m outputting all the attributes of articles. > >>> But i want to also output the extract for this article and result. > > >>> How would i do this? > > >>> article.extract doesn''t work, nor does article.results.extract > > >>> Thanks for your help. > > -- > Website:http://www.mooktakim.com > email: m...-lNzBllt/ka/iAt9p6pPDBw@public.gmane.org Zitierten Text ausblenden - > > - Zitierten Text anzeigen ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---