diff --git a/rails-models.md b/rails-models.md index 793f26980..edbdd4172 100644 --- a/rails-models.md +++ b/rails-models.md @@ -3,11 +3,84 @@ title: Rails Models layout: default --- -### Generating models +### [Query methods](http://devdocs.io/rails/activerecord/querymethods#method-i-order) + +```rb +items = Model + .where(first_name: "Harvey") + + .order(:title) + .order(title: :desc) + .order("title DESC") + + .reorder(:title) # discards other .orders + .rewhere(...) + + .limit(2) + .offset(1) + .uniq +``` + +### [FinderMethods](http://devdocs.io/rails/activerecord/findermethods) + +```rb +item = Model.find(id) +item = Model.find_by_email(email) +item = Model.where(email: email).first + +Model + .exists?(5) + .exists?(name: "David") + + .first + .last + .find_nth(4, [offset]) +``` + +### [Persistence](http://devdocs.io/rails/activerecord/persistence) + +```rb +item.new_record? +item.persisted? +item.destroyed? + +item.serialize_hash + +item.save +item.save! # Same as above, but raises an Exception + +item.update name: "John" +item.update! + +item.update_column :name, "John" # skips validations and callbacks +item.update_columns name: "John" +item.update_columns! name: "John" + +item.touch # updates :updated_at +item.touch :published_at + +item.valid? +item.invalid? + +item.destroy +item.delete # skips callbacks +``` + +```rb +Model.create # Same an #new then #save +Model.create! # Same as above, but raises an Exception +``` + +### Sorting + + +Generating +---------- $ rails g model User -### Associations +Associations +------------ belongs_to has_one @@ -192,37 +265,13 @@ Validation record.errors[:name].any? -API ---- +Other API +--------- - items = Model.find_by_email(email) - items = Model.where(first_name: "Harvey") - - item = Model.find(id) - - item.serialize_hash - item.new_record? - - item.create # Same an #new then #save - item.create! # Same as above, but raises an Exception - - item.save - item.save! # Same as above, but raises an Exception - - item.update - item.update_attributes - item.update_attributes! - - item.valid? - item.invalid? +### Callbacks * [Guides: callbacks](http://guides.rubyonrails.org/active_record_validations_callbacks.html) -### Sorting - - posts.order(:title) - posts.order("title DESC") - ### Mass updates # Updates person id 15 diff --git a/tomdoc.md b/tomdoc.md new file mode 100644 index 000000000..6d17ee19b --- /dev/null +++ b/tomdoc.md @@ -0,0 +1,59 @@ +--- +title: Tomdoc +--- + +``` +# Public: Duplicate some text an arbitrary number of times. +# +# text - The String to be duplicated. +# count - The Integer number of times to duplicate the text. +# +# Examples +# +# multiplex('Tom', 4) +# # => 'TomTomTomTom' +# +# Returns the duplicated String. +def multiplex(text, count) + text * count +end +``` + +### Tags + +``` +Deprecated +Internal +Public +``` + +### Options + +``` +# options - The Hash options used to refine the selection (default: {}): +# :color - The String color to restrict by (optional). +# :weight - The Float weight to restrict by. The weight should +# be specified in grams (optional). +``` + +### Yields + +``` +Yields the Integer index of the iteration. +Returns the duplicated String. +Returns nothing. +Raises Errno::ENOENT if the file can't be found. +Returns something else and this is a wrapped + multi-line comment. +``` + +### Signatures + +``` +# Signature +# +# find_by_[_and_...](args) +# +``` + +See [tomdoc.org](http://tomdoc.org/).