Returns value of primary field
Is generated by .mapping
macro.
Base abstract class for a database entity.
Returns adapter instance.
Implementation of AbstractClassMethods.all
.
Alias for .new
.
Alias for .new
.
Alias for .new
.
Returns criterion for column name of resource's table.
Returns QueryBuilder::ExpressionBuilder
object of this resource's table.
Returns adapter used to read resource from the database.
Returns star field statement for current resource's table.
Sets custom table name.
Returns resource's table name.
Starts database transaction.
Is a shortcut for .all.where
call.
Is a shortcut for .all.where
call.
Returns adapter used to write resource to the database.
Returns value of attribute name.
Returns a string containing a human-readable representation of object.
Returns value of primary field
Returns hash with model attributes; keys are symbols.
Returns a JSON string representing the resource.
Returns hash with model attributes; keys are strings.
Jennifer::Model::RelationDefinition
Jennifer::Model::Translation
Returns criterion for column name of resource's table.
User.c(:email) # => users.email
Returns QueryBuilder::ExpressionBuilder
object of this resource's table.
User.context.sql("ABS(1.2)")
Sets custom table name.
Specified table name should include table name prefix as it is used "as is".
Returns resource's table name.
User.table_name # "users"
Admin::User.table_name # "admin_users"
class Admin::Post < Jennifer::Model::Base
# ...
def self.table_prefix; end
end
Admin::Post.table_name # "posts"
Starts database transaction.
For more details see Jennifer::Adapter::Transactions
.
User.transaction do
Post.create
end
Is a shortcut for .all.where
call.
User.where { _name == "John" }
Returns value of attribute name.
It method doesn't invoke getter. If no attribute with given name is found - BaseException
is raised. To prevent this and return nil
instead - pass false
for raise_exception.
contact.attribute(:name) # => Jennifer::DBAny
contact.attribute("age") # => Jennifer::DBAny
contact.attribute(:salary) # => Jennifer::BaseException is raised
contact.attribute(:salary, false) # => nil
Returns a string containing a human-readable representation of object.
Address.new.inspect
# => "#<Address:0x7f532bdd5340 id: nil, street: "Ant st. 69", contact_id: nil, created_at: nil, updated_at: nil>"
Returns hash with model attributes; keys are symbols.
Is generated by .mapping
macro.
contact.to_h # => { name: "Jennifer", age: 2 }
Returns a JSON string representing the resource.
Without any argument or block passed in all resource columns are serialized.
user.to_json
# => {"id": 1,"name": "John Smith", "age": 42,"admin":false}
The only
argument allows to specify the exact collection of fields to be serialized:
user.to_json(only: %w[id name])
# => {"id": 1,"name": "John Smith"}
The except
argument allows to specify which field should not be serialized:
user.to_json(except: %w[id name])
# => {"age": 42,"admin":false}
Only one argument only
or except
can be specified at a time.
Also the block can be specified to serialize extra fields. As arguments block receives json builder and resource itself
user.to_json do |json|
json.field "first_name", user.name.split(" ")[0]
end
# => {"id": 1,"name": "John Smith", "age": 42,"admin":false, "first_name": "John"}
Returns hash with model attributes; keys are strings.
Is generated by .mapping
macro.
contact.to_h # => { "name" => "Jennifer", "age" => 2 }