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.
I would let rails handle this for you.
Add a
has_many :scores, :through => :suppliersto your account model, andbelongs_to :accountto your scores model, then load scores using: