Sunday, March 21, 2010

doing a survey with ruby on rails (part 2)

Now, that the tables and models are setup, we implement the View and Controller layers. The scaffold has given already some start, but we'll change these to give our application a better survey character.

First, the index view of the questions. We want to see a simple list of questions that we have so far, and the associated choices.

So, let's edit this view in /survey/app/views/question/index.html.erb. We remove almost everything and replace it with a unlinked list in html like this:



Note how we have used a second unlinked list to display the choices that are associated with a question, and how the modify and destroy actions are removed to be accessible only from inside the question edit. The question can be edited by clicking on it.

At the moment, we don't have any choices yet in our database. Let's add some with putting "http://0.0.0.0:3000/choices" in the URL of our browser.

I add the following for now: "it's great", "much", "ok", "not much", "green", "blue", "red", "yellow"

Now, we arrive at one of the more difficult parts. Putting checkboxes in the new and edit views of the questions.

First, the new action in /survey/app/views/questions/new.html.erb :

We need to iterate over the choices. We can do this like this:




We also insert the loop in the edit view:




To have a short list in our show view on choices, we add in app/views/questions/show.html.erb:



So, that was part 2. By now, you should have a survey app where an administrator can easily enter questions and associate possible choices with these questions.

Next, we need to have users who can take part in the survey.


Saturday, March 20, 2010

doing a survey with ruby on rails (part 1)

This is a basic tutorial that might help you understand the basics of ruby-on-rails. The idea of the project comes from this stackoverflow.com question. My current rails working environment is 2.3.5.

As with every rails project, in the beginning rails gives you a basic project setup using the model-view-controller pattern.

Let's try:


./ > rails -d mysql survey


The -d option is important to specify the usage of a database right at the start. In general, I use mysql for this, and the provided database.yml may need a bit tweeking the first time, but will soon be functional for many rails projects.

To create and test the database, you want to check with:


./survey/ > rake db:create


if there is no error message.

Coming back to our MVC pattern, at the moment, the directories /survey/app/models, /survey/app/views and /survey/app/controllers are still empty.

Basic resources (what the combination of a view-model-controller often is), can easily be done with scaffolding.

So, let's make a resource for our "question" and one resource for a "choice".


script/generate scaffold question whatabout:string
script/generate scaffold choice desc:string


With


rake db:migrate


we create our new tables in the database.

Now, we need to associate a choice wih a question. We want to select the choices that a user can enter in the survey. So, we use a m:n relationship between choices and questions. (The steps behind this are explained more in detail here, here and here).

So, to implement the associations, we need to modify our table and the models accordingly.

First, we create a helper table with:

script/generate model questions_choice


In the new migration, we add references to our "choices" and "questions" tables like this:

class CreateQuestionsChoices < ActiveRecord::Migration
def self.up
create_table :choices_questions, :id => false do |t|
t.references :choice, :question
t.timestamps
end
end

def self.down
drop_table :choices_questions
end
end


Let's check that we have no problems so far with a:

rake db:migrate


Next, we edit our models:

We say:


# question.rb
class Question < ActiveRecord::Base
has_and_belongs_to_many :choices
end

# choice.rb
class Choice < ActiveRecord::Base
has_and_belongs_to_many :questions
end

# questions_choice.rb
class QuestionsChoice < ActiveRecord::Base
belongs_to :question
belongs_to :choice
end


As a result, we should be able to access the table "Questions" from the table "Choices", and vice versa. "belongs_to", "has_and_belongs_to_many" are method calls of ActiveRecord. In a sense, Ruby let defines us our own domain-specific language easily, and that is basically what Rails is.

To show that the models are working, it is helpful to check with the Ruby interpreter first. For this, we start:


./survey/ > script/console -s


The -s option says we want to use a sandbox, i.e. our modifications in the database are rollbacked after we exit from the console.

So, first

q = Question.new
q.whatabout="Our Rails tutorial"
q.save


gives us a first question where we can add choices to. We do this with


c = Choice.new(:desc => "it's ok")
q.choices << c


Ruby knows from our models how to set the foreign key question_id and choice_id in the join table by using the operator "<<". We can repeat editing and adding new choices for our first question database.

Within the console, we can list all our choices for a question with:


q.choices.each {|choice| puts "#{choice.id} #{choice.desc}"}



In the next part of the tutorial, we will have a look on how we can provide a user interface for our models on questions and choices

Wednesday, December 16, 2009

network computers

web browsers are in essence some form network computers. computers can be a path to information as explained very nice in this talk by eric schmidt http://www.youtube.com/watch?v=cl8bEApvblg

he also explains that the information industry is actually much bigger than the IT industry, and that google sees itself as an information company. Personally, I think one ingredient in a company around information, is the ability to analyse data.

there is a nice book on data analysis here: http://www.google.de/#hl=de&source=hp&q=head+first+data+analysis&btnG=Google-Suche&meta=&aq=f&oq=head+first+data+analysis&fp=c6a75e802a8b0e7

Thursday, October 29, 2009

how can we get smarter?

how can we get smarter? maybe here is an interesting path:
Concept of "collaboratories"

also very interesting innovation marketing lecture by Clayton Christonson: Understand the job of what a customer is trying to accomplish

Christensen is very inspiring. Makes me popup the question if it is better to master the basics or to study with masters directly. This could make an interesting business called "growthcurve.com" where you could record what knowledge and insight you had to master to be at the point where you are now

Thursday, August 13, 2009

distributed applications

internet is all about pushing and pulling information from different places at different times. the basic idea behind this are the URL and the HTTP protocol. Also, the network gets encapsulated by the cloud concept.
An interesting variation on this seems the CouchDB project.
Here is an interesting video:
CouchDB overview

J. Chris Anderson often uses the REST concept and that he has been learning internet programming by "view source" HTML and later rails.

Here is a short tutorial on REST maybe interesting to look into one day:

PHP rest tutorial from yahoo

Sunday, August 09, 2009

webapp with hobo

Hobo sounds similar to holo and it makes me remember starship Enterprise. In any case, hobo is also a framework for making CRUD application with Rails and it helped me to deploy a first experiment of an app to Heroku: electric-moon-35.heroku.com

Some ressources were helpful for learning how to do this:
* First the the heroku quickstart docs
* The docs about debugging with Heroku
* A message in a mail server on problems with plugins and installing gems: NoMethodError undefined method
* That there exists something like and authentication problems with Herokugardens
* The increasingly popular GIT version control and GIT version control

Sunday, July 26, 2009

ruby experiments

wow.. had a really nice time about learning programming web-apps with rails on ruby:

* Heroku: Very convenient Hosting service heroku.com get in touch with cloud computing
* Railcasts: Very nice tricks to get ruby programs working: Railcasts.com
* Sample CRUD app: Vimeo tutorial by eric berry
* More rails tutorials: rails tutorials
* probably my first ruby book rails book

Monday, July 06, 2009

software and computers

wow, this talk is great.

why software is there, Moore's Law, entrepreneurship, relationship between software and engineering, software and business, online learning, access to information:

gates is a really a good speaker

Marketing, getting appraisal is a big part of Software.

Sunday, June 21, 2009

SQL podcasts

interesting list of SQL podcasts:

SQLDownUnder

Unit testing

eclipse webcast

especially towards the end this discussion gets interesting, testing JavaEE, Servlets, etc.

"Not care how a class does something, but only WHAT it is supposed to do."

Wednesday, June 17, 2009

ArrayList and parameter types

Interesting forum discussions on what it means to use a "heterogeneous collection" in Java:

forums.sun.com

Also, a reference book at google books on Java:

a google books/book on java

Saturday, June 13, 2009

limits of control

Tonight I saw a nice movie by Jim Jarmusch "The Limits of Control". The movie is based on projection and mirroring concepts. The main character is just a sort of wall that allows the viewer to enter the narrative directly.

Well, apart of very nice pictures including many pieces of art, the movie gives some clues of what make virtual worlds interesting. Virtual worlds represent toy models that allow viewers to interact in ways our real world does not allow.

Friday, April 17, 2009

catalogues = databases

I am studying for my database course M359 at the OU right now. And have to work on some lag in my study schedule. Insights so far:
1 a catalogue can be seen as a database. as such, a phone book, a prospectus, etc. can be seen as forms of databases
2 the logical views carries information which we would not have from hardware and OS perspectives on datastructures

The use of logical description in databases is a bit new to me. My understanding of logics in software always reduces to very abstract forms of symbolism, which is maybe too redustionistic here. The definition of logic in wikipedia is as follow:

Logic is the study of the principles of valid demonstration and inference. Logic is a branch of philosophy, a part of the classical trivium, as well as a branch of mathematics. The word derives from Greek λογική (logike), fem. of λογικός (logikos), "possessed of reason, intellectual, dialectical, argumentative", from λόγος logos, "word, thought, idea, argument, account, reason, or principle"


I have discovered an interesting book on the principles of database design from APress. The title is "Beginning database design" from Clare Chandler. As every good introduction, the book starts small with known matters and builds up complexity slowly. I found helpful to be reminded that simple Excel sheets are often simple forms of databases where data is collected in rows and columns. But only good database design allows to extract usefull information from many tables of data.

Sunday, April 05, 2009

Monday, February 02, 2009