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)
One 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.
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
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
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..
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)
end