DISQUS

e168f08: Protected: Assignment 3

  • jgn · 1 year ago
    Suggestions for "ObservationKind" --

    Folks, so the premise of the app is that people want to record things for various kinds of observations. So the one I've given here is: "personal weight."

    Can you suggest some more that you personally would want to track? Seriously. E.g.,

    -- number of hours practiced on your instrument
    -- hours spent with your eldest child one-on-one

    Stuff like that. It would be useful to know a good unit for the kind of observation: Is it most conventionally measured, say, in hours or minutes? And it is also interesting to know what the standard aggregation period is. Example:

    Kind: time practiced on your instrument
    Unit: minutes (i.e., when you enter a number for 10-Dec-2008, you would enter the number of minutes practiced for that observation)
    Period: per week

    So now I might enter in 10 minutes on Monday, 60 minutes on Tuesday, etc., etc., and then I'd be most interested in the total hours per week. At present the data model doesn't permit a recommended period for reporting, nor does it make it easy to have different units for the observation as opposed to the report, but all of that could be done.

    So what I'm after are more real-world kinds of measurement. If you add a comment along these lines, give:

    Kind:
    Unit:
    Period:

    I wouldn't mind having a little insight into why such a particular measurement would be useful to you.
  • student1 · 1 year ago
    Would this be something useful to track ?
    - The number of minutes/hours we spend do various task: attending meeting, walking dogs, driving, waiting in traffic.
    - The number of minutes/hours children spend playing games, watching TV or chatting with friends, reading
    - The number of hours spend on particular websites (facebook ?)
    - The number of hours spent on doing assignments, but the unit would be hours each week.

    But I think that the model can become complicated for "Kind" if one wants to aggregate and drill down along "Kind". If "Kind" is "relaxing activities", then one could include finer activities such as walking, cycling etc.. If one wants to ask about how much time I follow the news, then, it'll have to add up time listening to news on radio, plus reading websites, plus watching TV etc.. But different people may have a different model for "Kind" model. When we compare against "others", it is not so clear what that means.
  • jgn · 1 year ago
    These are all good. And the idea of breaking down something like "relaxing activities" into categories is interesting.
  • student1 · 1 year ago
    Looking at the zip in assignment 3, there is a db/migration/02_data_add_rollups.rb. It references a Rollup.new_from_seconds. Where is new_from_seconds ? I couldn't figure out what that code is doing. Ruby is still quite hard to read.
  • jgn · 1 year ago
    I wrote in the assignment (emphasis added):

    Rollup. Each rollup defines time intervals for the smallest unit of time; for this application, that smallest unit is an hour. Observations will be aggregated with the Rollup. *******More about this later.******* [i.e., in lecture.] We will be giving the code for the Rollup migration, the model, and the queries.

    and

    Notice that there is a Rollup model and that there are existing migrations 01 and 02 that manage this. Do not delete this stuff. Also, there is some code on the Observation model that picks the right Rollup to use, and there is a raw SQL query. Just put your validations and associations ahead of this code.

    c. You don’t have to create the rollups table or its Model! These are created for you by migrations 01 and 02. The reason they don’t use the standard Rails model numbers is that you can easily . . . .

    And then I talk about how to migrate up and down without deleting the rollup values.

    John
  • student1 · 1 year ago
    Thanks John,
    I'll try to follow the steps faithfully. I think that when I get to the end, I'll understand the part about th Rollup better. (In my lib subdirectory, there is no rollup.rb), but db:migrate VERSION=02 worked.

    In the meantime, I have a problem:
    I am at step schema_comparison.rb. The observation_sets table. The diagram (.jpg file) has a preferred_unit_id. But, it seems like the schema_comparison.rb code expects unit_id. So, I am going to change my use CreateObservationSets to have unit_id, unless you are expecting us to do something more complicated to map column names from preferred_unit_id to unit_id.

    Thanks,
  • jgn · 1 year ago
    The schema_comparison.rb is correct: use unit_id

    I will fix the diagram.

    *NOTE* The *association* will want to use "preferred_unit" -- I will show you in lecture how to do this so that even though the association will be called "prefered_unit" it will use the unit_id column.
  • student1 · 1 year ago
    I have an sqlite3 question. Is there a way to get out of sqlite3 easily after one gets the ...> prompt ?
    I now know about the .quit command.
    However, I keep forgetting the dot, and when that happens, I can the ...> prompt and it doesn't let me get out. I usually have to kill the process.
  • jgn · 1 year ago
    On Linux and Mac OSX, you should be about to press control D. This may work as well under Windows. If you're on Windows, tell us whether control D works or not.
  • student1 · 1 year ago
    Thanks. Control D works. I am on a Mac OS X.
  • student1 · 1 year ago
    I just want to try running to unit tests to see what happens:

    In running the unit test, I see:
    1) Failure:
    test_0020_new(MeasurementTest)
    [./test/unit/measurement_test.rb:14:in `test_0020_new'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
    <false> is not true.

    This test case tests that one cannot a measurement without a name.

    In my Measurement, I have:
    validates_uniqueness_of :name
    validates_presence_of :name

    I don't know why this would fail this particular test. Is there some way to see why this fail ?

    Is there a way to run just 1 test instead of the whole suite ?

    Thanks
  • student1 · 1 year ago
    I was trying script/console, I still have this problem.
    Inside the console, I have:

    Loading development environment (Rails 2.1.1)
    >> m = Measurement.new(:name => 'testmea' )
    => #<Measurement id: nil, name: "testmea", created_at: nil, updated_at: nil>
    >> m.save!
    => true
    >> m = Measurement.new
    => #<Measurement id: nil, name: nil, created_at: nil, updated_at: nil>
    >> m.save
    => true


    Is there some other way I can examine that the class does have validation of presence method,
    and that it is invoked when new is called ?
    I tried m.methods but that isn't what I want ?

    Thanks
  • jgn · 1 year ago
    Note that the Measurement class must be in app/models/measurement.rb -- did you put it there or in lib/ ?
  • student1 · 1 year ago
    :-) Yes, they are all in lib.
    (Now Rollup is there as well )

    Thanks.
  • jgn · 1 year ago
    The models should be in app/models !!
  • student1 · 1 year ago
    It works now ... thanks.

    But I will now have to go and change my migration script because they "require" from lib/user etc.
    But, I don't really know if it remembers whether this matter of not.
    It seems like the migration part is happy as long as there is a definition for user wherever I reference it.
  • jgn · 1 year ago
    Guess what? When you're running your migrations in Rails (with rake db:migrate) you should be able to leave out all of those requires! Rails will find everything automagically.
  • jgn · 1 year ago
    Before we talk about tests:

    Did you do ruby lib/schema_comparison.rb ?

    What was the result?

    John
  • student1 · 1 year ago
    It passed the schema test.
    Checking table 'measurements'
    Checking table 'observations'
    Checking table 'observation_sets'
    Checking table 'observation_kinds'
    Checking table 'units'
    Checking table 'users'
    Checking table 'rollups'

    I was following the steps.

    Thanks...
  • jgn · 1 year ago
    You can run just one file of tests like so:

    rake test:units TEST=test/unit/measurement_test.rb
  • student · 1 year ago
    After I completed rake test:units and received the following result

    Started
    ............................
    Finished in 0.734 seconds.

    28 tests, 33 assertions, 0 failures, 0 errors

    I copied files from extras/db/migrate directory to main db/migrate directory
    and I run rake db/migrate
    I received the following message:

    == 20081026024841 DataAddMeasurementsEtAl: migrating ========
    rake aborted!
    Validation failed: Name has already been taken
    I don't know why I received this error.
  • jgn · 1 year ago
    Could you double-check that you put the contents of extra/migrate in db/migrate ?

    (That is, you didn't, say, put the directory in so that you have db/migrate/migrate or some such.)

    I just did copied the migrations from the extra/migrate directory to a copy of the assignment's db/migrate directory -- worked like a charm.
  • student · 1 year ago
    I have them all in db/migrate not in db/migrate/migrate
  • student · 1 year ago
    I resolved the issue. Thank you.
  • jgn · 1 year ago
    What was it?
  • jgn · 1 year ago
    You might have manually added a Measurement with :name => nil by accident? Something like that?

    Hmm, or maybe the Demo app is conflicting. Did you run the Demo app first, and then the final migrations?
  • swithin · 1 year ago
    I have two failures and one error remaining. The two failures are nearly identical, and so I am looking for help on the first one:
    1) Failure:test_0020_new(UserTest)
    [./test/unit/user_test.rb:14:in `test_0020_new'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
    <false> is not true.

    I am validating the format of "login", and that "login" and "name" are unique, but those do not help... maybe they are the problem.

    Please assist.
  • jgn · 1 year ago
    Three suggestions:

    (1) The error -- meaning that the test can't run because an unexpected exception is being thrown -- is worth checking because sometimes such errors can be clues to why the failures are happening.

    (2) Write a separate little class and put it in the lib/ directory. Put the code that manipulates the models into a class method. For instance you might call the class CheckLogin and the method tryit:

    class CheckLogin
    def self.tryit
    u = User.new(:login = 'somethingunusual')
    u.save!
    end
    end

    To run a program like this in lib/ use script/runner from the root:

    script/runner "CheckLogin.tryit"

    NOTE: This will try it against your development database.

    Run it twice. The first time it should work because there is no user with the login 'somethingunusual'; the 2nd time it should not pass the validation because the login already exists.

    if this sequences either fails the first run, or works the 2nd time, then you need to review your code.

    (3) You might try removing your development database altogether (rm db/development.sqlite3) and running the migrations all over. Note that the 02 migration is slow. Then run the schema comparison again, and then the tests. It can happen that you've put something into the dev database that isn't getting removed during your "down" migrations, and this is screwing something up.
  • swithin · 1 year ago
    I found the problem, after trying all your suggestions. I had the test for format and uniqueness inside of the "name" method. Once I took them out, then those two "User" table failures were cured.

    Now I am onto that last remaining error:

    test_2000_assoc_9(UserTest):
    NoMethodError: undefined method `Observation' for #<Class:0x2b558a4>
    c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:1672:in `method_missing'
    C:/ruby/assignment03/app/models/user.rb:17:in `observations'
    ./test/unit/user_test.rb:32:in `test_2000_assoc_9'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'

    It seems that the count needing to be returned by the "observations" method (in the User class) has to join first to the "observation_set" table and then to the "observation" table. I am not sure how to do that in a method....

    I also would like your assistance in getting the "CheckLogin.tryit" script that you suggest working. That sounds like a valuable tool. My implementation appears to run, but it returns nothing.
  • jgn · 1 year ago
    When you write this: "It seems that the count needing to be returned by the "observations" method (in the User class) has to join first to the "observation_set" table and then to the "observation" table. I am not sure how to do that in a method...."

    Notice that in the instructions I write: "Note: Association #9 is only on User, and goes to Observation."

    Hint, hint: A user has many observations . . . through . . . something. You figure out the rest.
  • rajatbaner · 1 year ago
    Nice. This one was tricky. Had to root around in the book for a while!
  • Anita · 1 year ago
    When I run:

    ruby lib/schema_comparison.rb

    I get:

    Checking table 'measurements'
    Checking table 'observations'
    For the 'limit' attribute for column 'amount', expected '8' but you had ''
    For the 'precision' attribute for column 'amount', expected '8' but you had ''
    For the 'scale' attribute for column 'amount', expected '2' but you had ''
    For the 'sql_type' attribute for column 'amount', expected 'decimal(8,2)' but you had 'decimal'
    For the 'limit' attribute for column 'notes', expected '' but you had '255'
    For the 'sql_type' attribute for column 'notes', expected 'text' but you had 'varchar(255)'
    For the 'type' attribute for column 'notes', expected 'text' but you had 'string'
    Checking table 'observation_sets'
    Checking table 'observation_kinds'
    Checking table 'units'
    Checking table 'users'
    Checking table 'rollups'

    I've checked over my create table migration for 'observations' to make sure they match the schema and I don't see anything different from the other migrations. Also, when I run rake test:units I don't get any Failures or Errors and script/runner "Demo.run" gives me the result listed above. How do I fix the issues with the 'observations' table?

    Thanks.
  • Morris · 1 year ago
    I ran into similar issues with the schema, with slightly different error messages (different versions of sqlite? Windows vs. Mac?).
    The resolutions are:
    The schema in the jpg says that notes should be of type :string, but schema_comparison.rb is looking for type :text
    Although not specified in the schema diagram, the comparison test expects the amount to have a precision of 8 and a scale of 2.

    After changing t.string to t.text for :notes, and adding :precision and :scale options for amount, schema_comparison.rb stops complaining
  • jgn · 1 year ago
    Thanks -- fixed those up w/ a message at the top of the assignment.
  • jgn · 1 year ago
    Anita, everyone: That is just a flat-out error on my part. notes should be of type text, and amount should have precision 8 and scale 2. See the note at the very top.
  • Morris · 1 year ago
    With all of the tests passing (schema comparison and rake test:units), I tried running the demo program, following the instructions as given:
    I copied the data migrations into my db/migrate directory (directly, not in a sub-directory), and then tried running

    script\runner "Demo.run"

    converting the / to a \ because I'm on Windows. This didn't work, because DOS isn't hip to Unix-style "#!" directives.
    Looking inside the file and seeing the #!, I reran the command as

    ruby script/runner Demo.run

    and got a RuntimeError at demo.rb:29 in 'run':

    Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id (RuntimeError)

    The directions say to just copy the migrations into the right directory and then run the demo, so I assumed that the demo would invoke rake db:migrate.

    This turns out not to be the case. Here's what worked for me:

    1) Copy the data migrations from extras\db\migrate into db\migrate
    2) rake db:migrate
    3) ruby script/runner Demo.run
  • jgn · 1 year ago
    Thanks -- I left out that crucial line: run the migrations! Will fix. *DO* put Demo.run inside of quotation marks. You want to be able to put spaces into that param, so get in the habit of quoting what is being passed to script/runner
  • philadelphia · 1 year ago
    Hi,

    I'm pretty stuck on a couple of errors surrounding observation_sets... Does the solution involve the use of ":source"?


    Mike
  • jgn · 1 year ago
    Mike,

    Did you see this note at the end of the assignment description: "Note: One of the tricker associations is ObservationSet.preferred_unit - take a look at p. 340 in AWDR."

    That is the only thing in ObservationSet that is not vanilla. Basically, you want the ASSOCIATION to be preferred_unit while leveraging the underlying foreign key unit_id
  • philadelphia · 1 year ago
    Yup.. I had seen the note.... Maybe we're working off different book versions and page 340 is not the same in my book. I had looked it up, but did not see the relevance here.

    Mike
  • philadelphia · 1 year ago
    OK... got past the issue. I was trying to create a relationship from observation_set to observations, through units... I thought we need to do this based upon (mis) reading the diagram... Guess I should keep it right side up :-)

    When I removed this association, I am reduced to a single error... having to do with UnitTest. Debugging this now.

    Mike
  • philadelphia · 1 year ago
    simple error on the UnitTest... had set the scope within unit to the wrong field... all passing in <rake unit test> now. (woohoo :-))

    Thanks,
    Mike
  • jgn · 1 year ago
    Congrats!
  • douglashall · 1 year ago
    What software did you use to create the database schema image?
  • jgn · 1 year ago
    I used OmniGraffle for the Mac. (Actually OmniGraffle Pro.)

    The diagram for the "Child-Care Coop" over here -- http://e168f07.7fff.com/assignments/assignments... -- was done with Microsoft Visio.

    OmniGraffle is marginal for database diagrams. To do the Graffle diagram I had to download a user-contributed stencil -- http://graffletopia.com/stencils/318

    I haved a lot of these tools, and I guess looking at all of the different aspects -- use of use, appearance of the final document, etc. -- I'd have to say that Visio is the best. Also, for some databases, Visio can "reverse-engineer" a database: You connect to the database with ODBC, and Visio will suck all of the metadata out. It doesn't always work, though.
  • vaughanatworld · 1 year ago
    Might it be possible to get MilestoneII? It would be nice to get the jump on it while I have some extra cycles.
    Thanks
  • jgn · 1 year ago
    Possibly - let me get back to this topic in a bit.
  • Ana · 1 year ago
    Can we have the rest of the assignment?