From: Brian Candler Date: 2008-11-11T22:14:27+09:00 Subject: Re: implementing mvc - using observer pattern - beginner to Adam Akhtar wrote: > class DB > .. > .. > > def insert (task) > ... > #some kirybbase calls here. > .. > end > > end In a typical MVC, the model would be a class which represents the task, rather than a class which represents the database connection (the latter would be hidden away behind it). Hence something like: t = Task.new t.name = "Paint shed" t.save! ... t = Task.find_by_name("Paint shed") t.completed_at = Time.now t.save! If you only have a DB class, then the controller can talk to it directly, but you've lost the "M" from MVC. This may be no loss - simple models are often dumb and just map directly to SQL rows. But sometimes it's useful to put logic in the model layer, e.g. def full_name "#{first_name} #{last_name}" end def full_name=(name) f, l = name.split(" ", 2) self.first_name = f self.last_name = l end Here we have a virtual accessor that makes it look like the model has a full_name column, even though the database actually has two separate columns, first_name and last_name. > class UI > > blahblahblah > > def menu > puts "|A| to add a task" > ... > ... > end > > def navigation > menu > input = gets.chomp > case input > when "A" > add task > etc > etc > etc > end If you merge the view into the controller then you might get: class TasksController def list tasks = Task.find(:all) puts "You have #{tasks.size} tasks" tasks.each { |t| puts t.name } end def show(n) task = Task.find(n) puts "Showing task #{n}" puts t.name end def create print "Enter task name:" name = gets.chomp print "Enter completion date:" date = gets.chomp t = Task.new t.name = name t.date = date t.save! show(t.id) end end But what's missing is the sequencing: after showing task n there may be associated actions (e.g. edit task n, delete task n, return to listing) Maybe a good way to approach this is to start as a simple Rails app with a sqlite3 backend. Then you can consider how best to build the UI part which will (a) show the current view, and (b) prompt for next action. -- Posted via http://www.ruby-forum.com/.