#counter_cache in father table sons_count:integer in son model belongs_to :father,:counter_cache => true
#select a category or create one attr_accessor :new_category_one #virtual attribute before_save a record: method_name def method_name create_fatherModelName(:name => new_category_one) unless new_category_one.blank? end #and valid is neccessary def validate if model_id.blank? && new_level_one.blank? errors.add(:attr,:message =>"...") end end #in views <p> <%= f.label :level %><br /> <%= f.collection_select(:level_id, Level.find(:all,:order => :id), :id, :name,:prompt => "--select a level--") %> or Create: <%= f.text_field :new_level_one %> </p>
#paginate cattr_reader :per_page #class variable @@per_page = 15 def self.search(search,page) paginate(:page => page, :order => ["league_id,name"], :conditions => ["name like ?","%#{search}%"]) end #can be be do this @category.@papers.paginate(:page => params[:page]) #hidden attr per_page use class variable
#Player.largethan returns an array includes player's age > 20 named_scope :largethan,:conditions => ["age > ?", 20 ]
#Paper belongs_to Category,then Category has hidden attribute: category.paper_ids #this is a array,in view can use like this <%= check_box_tag "category[product_ids][]", product.id, @category.products.include?(product)%> <%= product.name %> #in controller action update @category = Category.find(params[:id]) params[:category][:product_ids]||=[] #in new is not need like this @category = Category.new(params[:category]) #would be ok
#yield and content_for #yield can be used in layout <%= yield :one %> # action's html <% content_for do %> #code <% end %>