| Class | Rfm::Result::Record |
| In: |
lib/rfm_result.rb
|
| Parent: | Rfm::Util::CaseInsensitiveHash |
The Record object represents a single FileMaker record. You typically get them from ResultSet objects. For example, you might use a Layout object to find some records:
results = myLayout.find({"First Name" => "Bill"})
The results variable in this example now contains a ResultSet object. ResultSets are really just arrays of Record objects (with a little extra added in). So you can get a record object just like you would access any typical array element:
first_record = results[0]
You can find out how many record were returned:
record_count = results.size
And you can of course iterate:
results.each (|record|
// you can work with the record here
)
You can access field data in the Record object in two ways. Typically, you simply treat Record like a hash (because it is a hash...I love OOP). Keys are field names:
first = myRecord["First Name"] last = myRecord["Last Name"]
If your field naming conventions mean that your field names are also valid Ruby symbol named (ie: they contain only letters, numbers, and underscores) then you can treat them like attributes of the record. For example, if your fields are called "first_name" and "last_name" you can do this:
first = myRecord.first_name last = myRecord.last_name
Note: This shortcut will fail (in a rather mysterious way) if your field name happens to match any real attribute name of a Record object. For instance, you may have a field called "server". If you try this:
server_name = myRecord.server
you’ll actually set server_name to the Rfm::Server object this Record came from. This won’t fail until you try to treat it as a String somewhere else in your code. It is also possible a future version of Rfm will include new attributes on the Record class which may clash with your field names. This will cause perfectly valid code today to fail later when you upgrade. If you can’t stomach this kind of insanity, stick with the hash-like method of field access, which has none of these limitations. Also note that the +myRecord[]+ method is probably somewhat faster since it doesn’t go through method_missing.
If you have a repeating field, RFM simply returns an array:
val1 = myRecord["Price"][0] val2 = myRecord["Price"][1]
In the above example, the Price field is a repeating field. The code puts the first repetition in a variable called +val1+ and the second in a variable called +val2+.
If the ResultSet includes portals (because the layout it comes from has portals on it) you can access them using the Record::portals attribute. It is a hash with table occurrence names for keys, and arrays of Record objects for values. In other words, you can do this:
myRecord.portals["Orders"].each {|record|
puts record["Order Number"]
}
This code iterates through the rows of the Orders portal.
RFM automatically converts data from FileMaker into a Ruby object with the most reasonable type possible. The type are mapped thusly:
In addition to portals, the Record object has these useful attributes:
| mod_id | [R] | |
| portals | [R] | |
| record_id | [R] |
Gets the value of a field from the record. For example:
first = myRecord["First Name"] last = myRecord["Last Name"]
This sample puts the first and last name from the record into Ruby variables.
You can also update a field:
myRecord["First Name"] = "Sophia"
When you do, the change is noted, but *the data is not updated in FileMaker*. You must call Record::save or Record::save_if_not_modified to actually save the data.
Saves local changes to the Record object back to Filemaker. For example:
myLayout.find({"First Name" => "Bill"}).each(|record|
record["First Name"] = "Steve"
record.save
)
This code finds every record with Bill in the First Name field, then changes the first name to Steve.
Note: This method is smart enough to not bother saving if nothing has changed. So there’s no need to optimize on your end. Just save, and if you’ve changed the record it will be saved. If not, no server hit is incurred.