Skip to content

Instantly share code, notes, and snippets.

@lscott3
Created March 26, 2012 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lscott3/2203389 to your computer and use it in GitHub Desktop.
Save lscott3/2203389 to your computer and use it in GitHub Desktop.
Builder
class Account < ActiveRecord::Base
belongs_to :package
has_many :users
has_many :clients
has_many :items
has_many :attributes
has_many :notes
accepts_nested_attributes_for :users
accepts_nested_attributes_for :clients
accepts_nested_attributes_for :attributes
accepts_nested_attributes_for :notes
accepts_nested_attributes_for :items
authenticates_many :user_sessions, :scope_cookies => true, :find_options => { :limit => 1 }
end
class Item < ActiveRecord::Base
has_many :attributes
has_many :notes, :as => :noteable
belongs_to :account
belongs_to :client
accepts_nested_attributes_for :attributes
accepts_nested_attributes_for :notes
before_save :create_service_tag
def create_service_tag
if service_tag.blank?
self.service_tag = SecureRandom.hex(3)
end
end
end
class ItemsController < ApplicationController
before_filter :require_user, :except => [:search]
def new
@item = current_user.account.items.build
@item.notes.build #this line here
3.times { @item.attributes.build }
end
end
<h1>New Item</h1>
<%= form_for @item do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :attributes do |a| %>
<%= a.label :name %>
<%= a.text_field :name %>
<%= a.label :value %>
<%= a.text_field :value %>
<% end -%>
<%= f.fields_for :notes do |note| %>
<%= note.label :note %>
<%= note.text_field :note %>
<% end -%>
<%= f.submit "Create Item" %>
<% end -%>
class Note < ActiveRecord::Base
belongs_to :account
belongs_to :noteable, :polymorphic => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment