Module | AWS::Record::AbstractBase::ClassMethods |
In: |
lib/aws/record/abstract_base.rb
|
Creates an object (or multiple if you pass an array of attributes). The {save} method is called on the object(s) after construction. The object(s) are returned wether or not the object(s) are valid.
class Book < AWS::Record::Model string_attr :title end book = Book.create(:title => "The big book of tests") book.persisted? #=> true books = Book.create([{:title => 'abc'}, {:title => 'xyz'}]) books.each(&:persisted?) #=> [true, true]
Creates an object (or multiple if you pass an array of attributes). The {save!} method is called on the object(s) after construction. If the object(s) are not valid, then an error is raised.
class Book < AWS::Record::Model string_attr :title validates_presence_of :title end book = Book.create!(:title => "The big book of tests") book.persisted? #=> true book = Book.create!() #=> raises AWS::Record::InvalidRecordError
Adds a scoped finder to this class.
class Book < AWS::Record::Model scope :top_10, order(:popularity, :desc).limit(10) end Book.top_10.to_a #=> [#<Book...>, #<Book...>] Book.top_10.first #=> #<Book...>
You can also provide a block that accepts params for the scoped finder. This block should return a scope.
class Book < AWS::Record::Model scope :by_author, lambda {|name| where(:author => name) } end # top 10 books by the author 'John Doe' Book.by_author('John Doe').top_10
@param [Symbol] name The name of the scope. Scope names should be
method-safe and should not conflict with any other class methods.
Allows you to override the default shard name for this class. The shard name defaults to the class name. @param [String] name