Assignment 3: Milestone II
Here are some finds that I suggest you write prefatory to implementing the version of MetricsMine you will be submitting for a grade. Remember to use the version of MetricsMine I put on the downloads page that has the correct migrations and associations.
It turns out that the “finds” you must do for this application are exceedingly simply as compared to last year’s ChildCare Co-op. The reason is that MetricsMine is preoccupied with data display and not so much with more complicated use cases.
- Find all observation sets for a user with id user_id (in the code the user id will come from the session, i.e., session[:user_id]).
- Find all observations for a particular observation set; order the observations by the ‘created_at’ column, from oldest to newest.
- Find all observations for a particular observation set; order the observations from newest to oldest.
- Find all observations for a particular observation set; order the observations by the ‘created_at’ column, from oldest to newest. Ensure that the observation_set is owned by a particular user.
- Find all observations for a particular observation set; order the observations from newest to oldest. Ensure that the observation_set is owned by a particular user.
- Find the “first” unit of the measurement of an observation kind for a particular observation set. (You may quietly ignore how “first” is defined, i.e., the measurement may have more than one unit; you don’t have to get the “first” one by created_at or any other specific ordering.)
The following is not required, but I will be using it for the implementation of “compare” which I will pass out:
- Find observation sets own by other users that have the same observation kind.
blog comments powered by Disqus
Add New Comment
Viewing 13 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Mike
Do you already have an account? Log in and claim this comment.
The "finds" this semester are quite easy, and most of them will be very similar to find requests in the standard "CRUD" operations in LinkWizz, CCC, etc.
If you feel confident, you can just move right on to Milestone III -- you will have to use these various "finds" in the implementation, but doing them separately is just intended to be a help along the way, nothing more.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Question 1) Why am I having to used nested-hash syntax in order to access the :login value from form_for?
# if params[:user][:login] == User.find(:first).login #THIS WORKS.
# if params[:login] == User.find(:first).login #THIS DOES NOT. WHY? THIS IS SUGGESTED SYNTAX.
Question 2) Why is the login method failing to find a valid :login value in the database. ( In script/console I can use a slightly modified syntax [replacing the params argument with the login string] to easily retrieve the user record.)
if params[:user][:login] == User.find(:first, :conditions => ["login= ?" , params[:user][:login]]) #NOPE
# if params[:login] == User.find(:first, :conditions => ["login= ?" , params[:login]]) #NOPE
Do you already have an account? Log in and claim this comment.
If you're asking about the Linkwizz application, which seems likely based on the symbols you're using here, this has been answered in the comments on the download page. There's a bug in the login code, which is looking at params[:login] when it should be looking at params[:user], because the form is a form_for :user. See section 22.4, 22.5, etc. in the AWDR textbook, and also review the lecture slides about forms. There's also some discussion about this in the comments to Milestone III.
<% form_for :foo >
will give you a hash in params[:foo], with each key in the hash being the symbol for a field in the form. This is the way Rails is designed, and it lets you pass the entire hash, containing all of the data entered into the form, to the 'new' method for the model class, if you've created your form in the conventional way, with each form field corresponding to an attribute of the model object.
I don't know where you're getting "SUGGESTED SYNTAX" that skips the model class name, but it's incorrect.
Question 2: params[:user][:login] is a string. User.find() returns an object of class User. Without some additional code in the User class, these two expressions can't compare as equal.
Note also that 'find' is cleverly defined to accept a hash for the :conditions, similar to the way 'new' takes a hash. So if the login is the only thing in the form (as it is in the Linkwizz application), then the first version of User.find you've written in your Question 2 is equivalent to the terser
User.find(:first, :conditions => params[:user])
which seems to be more idiomatic Rails.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
<input type='text' name='model[attribute]'>
and this is converted to a key in the params hash that looks like params[model][attribute]. The hashes can nest even deeper -- it depends on how you construct your form.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
* Milestone I:
ZIP; Example solution to Milestone I (migrations and associations: ZIP
You want the second ZIP. I just did. In, for example, app/models/measurement.rb, you see the following:
class Measurement < ActiveRecord::Base
has_many :units # 7b
has_many :observation_kinds # 6b
validates_presence_of :name
validates_uniqueness_of :name
end
In, for instance, db/migrate/20081025201508_create_measurements.rb you will find:
class CreateMeasurements < ActiveRecord::Migration
def self.up
create_table :measurements do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :measurements
end
end
I'm not sure what you're doing. If there is something that is not clear on the downloads page, let me know.
Do you already have an account? Log in and claim this comment.
Thanks
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment