Module: MassiveRecord::ORM::Relations::Interface::ClassMethods
- Defined in:
- lib/massive_record/orm/relations/interface.rb
Instance Method Summary collapse
-
#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
#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 |