Module: MassiveRecord::ORM::Relations::Interface::ClassMethods
- Defined in:
- lib/massive_record/orm/relations/interface.rb
Instance Method Summary collapse
-
#embedded_in(name, *args) ⇒ Object
Embedded in is being used together with embeds_many on the other side of such a relation.
-
#embeds_many(name, *args) ⇒ Object
Used to defined a relationship to other models where the other models are embedded inside of owner record.
-
#references_many(name, *args) ⇒ Object
Used to define a reference many relation.
-
#references_one(name, *args) ⇒ Object
Used to define a references one relation.
Instance Method Details
#embedded_in(name, *args) ⇒ Object
Embedded in is being used together with embeds_many on the other side of such a relation.
class Address < MassiveRecord::ORM::Embedded
:person
end
You can also pass in :polymorphic => true as an option. If you do so here is an example:
class Person < MassiveRecord::ORM::Table
:addresses, :inverse_of => :addressable
end
class Address < MassiveRecord::ORM::Embedded
:addressable, :inverse_of => :addresses, :polymorphic => true
end
156 157 158 159 160 |
# File 'lib/massive_record/orm/relations/interface.rb', line 156 def (name, *args) = set_up_relation('embedded_in', name, *args) .owner_class = self () end |
#embeds_many(name, *args) ⇒ Object
Used to defined a relationship to other models where the other models are embedded inside of owner record.
class Person < MassiveRecord::ORM::Table
:addresses
end
The embeds many association gets one column family per association. embeds_many :addresses will by default be stored in the addresses column family. You can however do this: embeds_many :addresses, :store_in => :base to manipulate the column family it is stored within.
Embedded records gets a composite key consisting of base_class and the record’s id. This is how it is possible to mix embedded collection in to one column family / existing family with “normal” attributes. Please mind however, doing such a mixing might get you into trouble if you have attribute names which looks like an embedded address key. Companybook wanted this option, as they said haveing multiple column family might slow down Hbase.
Attributes will be serialized by the Base.coder, by default will be JSON, but it really can be anything. The way records are stored inside of a column family will be:
| key | attributes |
| “address|id1” | { :street => “Askerveien”, :number => “12”, etc… }
Options, all optional:
<tt>class_name</tt>:: Class name is calculated from name, but can be overridden here.
<tt>store_in</tt>:: Send in the column family to store foreign key in. If none given,
129 130 131 132 133 134 |
# File 'lib/massive_record/orm/relations/interface.rb', line 129 def (name, *args) = set_up_relation('embeds_many', name, *args) .owner_class = self add_column_family(.store_in) () end |
#references_many(name, *args) ⇒ Object
Used to define a reference many relation. Example of usage:
class Person < MassiveRecord::ORM::Table
column_family :info do
field :name
end
references_many :cars, :store_in => :info
end
First argument is the name of the relation. class_name and attribute for foreign keys are calculated from it, if noen given. In the example above Person records will have attribute cars_ids which will be an array populated with foreign keys.
Options, all optional:
<tt>class_name</tt>:: Class name is calculated from name, but can be overridden here.
<tt>foreign_key</tt>:: Foreign key is calculated from name suffixed by _ids as default.
<tt>store_in</tt>:: Send in the column family to store foreign key in. If none given,
you should define the foreign key method in class if it can be
calculated from another attributes in your class.
<tt>records_starts_from</tt>:: A method name which returns an ID to start from when fetching rows in
Person's table. This is useful if you for instance has a person with id 1
and in your table for cars have cars id like "<person_id>-<incremental number>"
or something. Then you can say references_many :cars, :starts_with => :id.
<tt>find_with</tt>:: Assign it to a Proc and we will call it with the proxy_owner if you need complete
control over how you retrieve your record.
As a default TargetClass.find(foreign_keys_method) is used.
Example usage:
person = Person.first person.cars # loads and returns all cars. person.cars.first # Returns first car, either by loading just one object, or return first object in loaded proxy. person.cars.find(“an_id”) # Tries to load car with id 1 if that id is among person’s cars. Either by a query and look among loaded records person.cars.limit(3) # Returns the 3 first cars, either by slice the loaded array of cars, or do a limited DB query.
90 91 92 93 |
# File 'lib/massive_record/orm/relations/interface.rb', line 90 def references_many(name, *args) = set_up_relation('references_many', name, *args) create_references_many_accessors() end |
#references_one(name, *args) ⇒ Object
Used to define a references one relation. Example of usage:
class Person < MassiveRecord::ORM::Table
column_family :info do
field :name
end
references_one :boss, :class_name => "Person", :store_in => :info
end
First argument is the name of the relation. class_name and foreign key is calculated from it, if none given.
Options, all optional:
<tt>class_name</tt>:: Class name is calculated from name, but can be overridden here.
<tt>polymorphic</tt>:: Set it to true for make the association polymorphic. Will use foreign_key,
remove the "_id" (if it's there) and add _type for it's reading/writing of type.
<tt>foreign_key</tt>:: Foreign key is calculated from name suffixed by _id as default.
<tt>store_in</tt>:: Send in the column family to store foreign key in. If none given,
you should define the foreign key method in class if it can be
calculated from another attributes in your class.
<tt>find_with</tt>:: Assign it to a Proc and we will call it with the proxy_owner if you need complete
control over how you retrieve your record.
As a default TargetClass.find(foreign_key_method) is used.
41 42 43 44 45 46 |
# File 'lib/massive_record/orm/relations/interface.rb', line 41 def references_one(name, *args) = set_up_relation('references_one', name, *args) create_references_one_accessors() create_references_one_polymorphic_accessors() if .polymorphic? end |