Reputation: 13012
hey all i am stumped by this query and have thought it over and cant seem to put my head on it.
i have three tables, account, suppliers and scores
an account has many suppliers and a supplier has many scores.
what i would like to do is display all the scores that have been changed or created within a two week period that pertain to an account. i dont want to pull the whole db as it has over 2 million lines of code so i want to leave the calculations to be preformed by sql.
the trouble is making a clear sql statement.
this is my thinking.
sql=""
scores = ActiveRecord::Base.connection.execute(sql)
@scorecards = []
@scorecards {|sc| sc << scores}
this way i can save all my results in a array and ill be able to work with them from there.
my sql statement looks like this:
SELECT * FROM sms_development.scores WHERE DATE(created_at) = CURDATE() - INTERVAL 14 DAY AND CURDATE() OR DATE(updated_at) = CURDATE() - INTERVAL 14 DAY AND CURDATE();
now here is where i am confussed i have to run this statement for each scorecard, then match it to the score_id in the suppliers table and then match the suppliers id to the the suppliers_id in the accounts table.
what is the best way of accomplishing this within the SQL query?? sorry if its a simple question i am new to MySQL. and this seems like it could get hairy.
Upvotes: 0
Views: 185
Reputation: 668
I would let rails handle this for you.
Add a has_many :scores, :through => :suppliers
to your account model, and belongs_to :account
to your scores model, then load scores using:
Scores = @account.scores.where("created_at > ? Or updated_at > ?", 2.weeks.ago, 2.weeks.ago)
Upvotes: 1