I have made 3 tables +------------+ +----------------+ +------------+ |people | |people_campaigns| |campaigns | +------------+ +----------------+ +------------+ |id :int | <----> |people_id :int | /-> |id :int | |name :string| |campaign_id :int| <-/ |name :string| |etc ... | |exported :int | |desc :string| +------------+ +----------------+ +------------+ The people model looks like this has_many :people_campaigns has_many :campaigns, :through => :people_campaigns the other models I left empty because there is no need for me to work the other way I think What I want to do is make an export of all people that are in campaign "x" and are not yet exported. If they are exported then update with people_campaigns.exported=1 what I did is (except for the fastercsv lines): people = Person.find(:all) people.each do |person| campaigns = person.campaign campaings.each do |campaign| if campaign.name == params[:campaignName] tester = PeopleCampaign.find(:first, :conditions => ["person_id=? and campaign_id=? and isnull(exported)",person.id,campaign.id] ) if tester #print all to fastercsvlines tester.exported = 1 tester.save end end end end This part is out of the top of my head but I don''t like this code and I think there is a nicer and better (ruby on rails) way to do this ??? especially the part where i lookup the peoplecampaign record for which i had to add an id (yeah didn''t mention it in the table layout) -- 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 Feb 12, 11:00 am, Daniel Van den oord <rails-mailing-l...@andreas- s.net> wrote:> I have made 3 tables > > +------------+ +----------------+ +------------+ > |people | |people_campaigns| |campaigns | > +------------+ +----------------+ +------------+ > |id :int | <----> |people_id :int | /-> |id :int | > |name :string| |campaign_id :int| <-/ |name :string| > |etc ... | |exported :int | |desc :string| > +------------+ +----------------+ +------------+ > > The people model looks like this > > has_many :people_campaigns > has_many :campaigns, :through => :people_campaignsOne thing which I notice is that you have two options: has_many :campaigns, :through => relationships (choose a better model name than "relationship") Or: has_and_belongs to :campaigns_people with campaigns first. At least, that''s my understanding. I don''t think that there''s anything technically wrong for a hmt relationship to use "campaigns_people", but it''s not the convention. -Thufir --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
My main problem is with the part that it loops twice and does a seperate lookup. I am worried that this had major performance problems because the Person table will hold 1.2 million records and at least 700.000 people per campaigns there will be 4 at least this means that the people_campaigns table will grow to about 2.8 million records and perhaps more The naming convention I got from the rails wiki but I will look into this however this isn''t really the problem -- 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 2/13/08, Daniel Van den oord <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > My main problem is with the part that it loops twice and does a seperate > lookup. I am worried that this had major performance problems because > the Person table will hold 1.2 million records and at least 700.000 > people per campaigns there will be 4 at least this means that the > people_campaigns table will grow to about 2.8 million records and > perhaps more > > The naming convention I got from the rails wiki but I will look into > this however this isn''t really the problemLet''s set this up conventionally: class Person < ActiveRecord::Base has_many :campaigns :through => :campaign_enrollments has_many :campaign_enrollments # The people table needs at least id as well as any other attributes. end class CampaignEnrollment belongs_to :person belongs_to :campaign # The campaign_enrollments table needs at least id, person_id, campaign_id and exported fields. end class Campaign has_many :campaign_enrollments has_many :people :through => :campaign_enrollments # the campaigns table needs at least id field plus other campaign attributes. end Now given a campaign campaign = Campaign.find(...) campaign.campaign_enrollments.find(:all, :include => [:people, ;campaigns] :conditions => ["exported !=?", true]).each do | campaign_enrollment| # whatever exporting does here campaign_enrollment.update_attribute(:exported, true) end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 Feb 13, 3:13 pm, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...]> campaign.campaign_enrollments.find(:all, :include => [:people, > ;campaigns] :conditions => ["exported !=?", true]).each do | > campaign_enrollment| > # whatever exporting does here > campaign_enrollment.update_attribute(:exported, true) > end[...] This sort of iteration couldn''t go in the controller, could it? It would have to go in the view? -Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If I understood it correctly.. almost eveything that can be in the controller can be done in the view (with some exceptions) however this example is being used for an import and export the export doesn''t have a view I use fastercsv to generate a csv file streaming to the browser.. Thufir wrote:> On Feb 13, 3:13 pm, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > [...] >> campaign.campaign_enrollments.find(:all, :include => [:people, >> ;campaigns] :conditions => ["exported !=?", true]).each do | >> campaign_enrollment| >> # whatever exporting does here >> campaign_enrollment.update_attribute(:exported, true) >> end > [...] > > This sort of iteration couldn''t go in the controller, could it? It > would have to go in the view? > > > -Thufir-- 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 13 Feb 2008, at 15:13, Rick DeNatale wrote:> > On 2/13/08, Daniel Van den oord <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> My main problem is with the part that it loops twice and does a >> seperate >> lookup. I am worried that this had major performance problems because >> the Person table will hold 1.2 million records and at least 700.000 >> people per campaigns there will be 4 at least this means that the >> people_campaigns table will grow to about 2.8 million records and >> perhaps more >> >> The naming convention I got from the rails wiki but I will look into >> this however this isn''t really the problem > > Let''s set this up conventionally: > > class Person < ActiveRecord::Base > has_many :campaigns :through => :campaign_enrollments > has_many :campaign_enrollments > > # The people table needs at least id as well as any other > attributes. > end > > class CampaignEnrollment > belongs_to :person > belongs_to :campaign > # The campaign_enrollments table needs at least id, person_id, > campaign_id and exported fields. > end > > class Campaign > has_many :campaign_enrollments > has_many :people :through => :campaign_enrollments > # the campaigns table needs at least id field plus other campaign > attributes. > end > > Now given a campaign > > campaign = Campaign.find(...) > > campaign.campaign_enrollments.find(:all, :include => [:people, > ;campaigns] :conditions => ["exported !=?", true]).each do | > campaign_enrollment| > # whatever exporting does here > campaign_enrollment.update_attribute(:exported, true) > endit might be nice to add has_many :unexported_enrollments, :conditions => ["exported != ?", true], :class_name => "CampaignEnrollment" to campaign. you could then write campaign.unexported_enrollments.find :all, :include => [...] do | campaign_enrollment| ... end This will do exactly the same thing, but can make things so much more readable. Fred> > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.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 Thu, Feb 14, 2008 at 10:05 AM, Daniel Van den oord <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > If I understood it correctly.. > almost eveything that can be in the controller can be done in the view > (with some exceptions)But it really should be done in the model, with a method used to allow the controller to trigger it. Putting too much business logic in a controller is a bad code smell. Putting too much logic in views is an even bigger bad code smell. http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 Thu, Feb 14, 2008 at 10:15 AM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> it might be nice to add > has_many :unexported_enrollments, :conditions => ["exported != ?", > true], :class_name => "CampaignEnrollment" > to campaign.or even has_many :enrollments, :class_name => "CampaignEnrollment" do def unexported find(:all, :conditions => ["exported !=>", true] end Which would allow campaign.enrollments.unexported.each do | enrollment | ... has_many :enrollments> you could then write > > campaign.unexported_enrollments.find :all, :include => [...] do | > campaign_enrollment| > ... > end > > This will do exactly the same thing, but can make things so much more > readable. > > > Fred > > > > > > > > > -- > > Rick DeNatale > > > > My blog on Ruby > > http://talklikeaduck.denhaven2.com/ > > > > > > > > > > > >-- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---