Skip to content
Browse files

Add tasks

  • Loading branch information...
1 parent 5ef870b commit d2d85cff71d92ff182556d0bc1a94cacbe86b5b1 @xaviershay committed Mar 7, 2011
View
3 app/helpers/form_helper.rb
@@ -5,6 +5,9 @@ def setup_user(user)
user.interest_users.build(:interest => interest)
end
user.interest_users.sort_by! {|x| x.interest.name }
+
+ 3.times { user.tasks.build }
+
user
end
end
View
5 app/models/task.rb
@@ -0,0 +1,5 @@
+class Task < ActiveRecord::Base
+ belongs_to :user
+
+ validates_presence_of :name
+end
View
2 app/models/user.rb
@@ -2,9 +2,11 @@ class User < ActiveRecord::Base
has_one :address
has_many :interest_users
has_many :interests, :through => :interest_users
+ has_many :tasks
accepts_nested_attributes_for :address
accepts_nested_attributes_for :interest_users, :allow_destroy => true
+ accepts_nested_attributes_for :tasks, :allow_destroy => true, :reject_if => :all_blank
validates_presence_of :email
end
View
15 app/views/users/_form.html.erb
@@ -25,13 +25,26 @@
<%= f.fields_for :interest_users do |ff| %>
<div>
- <%= ff.hidden_field :id %>
<%= ff.hidden_field :interest_id %>
<%= ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0', '1' %>
<%= ff.label :_destroy, ff.object.interest.name %>
</div>
<% end %>
+ <h2>Tasks</h2>
+ <%= f.fields_for :tasks do |ff| %>
+ <div>
+ <%= ff.label :name %>
+ <%= ff.text_field :name %>
+
+ <% if ff.object.persisted? %>
+ <%= ff.check_box :_destroy %>
+ <%= ff.label :_destroy, "Destroy" %>
+ <% end %>
+ </div>
+
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
View
6 db/migrate/20110306221257_create_models.rb
@@ -24,6 +24,12 @@ def self.up
t.references :user
t.references :interest
end
+
+ create_table :tasks do |t|
+ t.references :user
+
+ t.string :name, :null => false
+ end
end
def self.down
View
5 db/schema.rb
@@ -26,6 +26,11 @@
t.string "name", :null => false
end
+ create_table "tasks", :force => true do |t|
+ t.integer "user_id"
+ t.string "name", :null => false
+ end
+
create_table "users", :force => true do |t|
t.string "email", :null => false
t.datetime "created_at"

0 comments on commit d2d85cf

Please sign in to comment.
Something went wrong with that request. Please try again.