Skip to content

Lesson: Using typed predicates in your models (AF7)

Jim Coble edited this page Apr 25, 2014 · 3 revisions

Sometimes it is enough to fill in a string containing a date for the created_date, but in this case, let's say we want to store the field into solr with a date type. We can declare the type of a field by giving the field a block that asserts the type is :date:

class DublinCoreDatastream < ActiveFedora::NtriplesRDFDatastream
  property :title, predicate: RDF::DC.title
  property :created, predicate: RDF::DC.created do |index|
    index.as :stored_searchable
    index.type :date
  end
  #...
end
class MyObj < ActiveFedora::Base
  has_metadata 'descMetadata', type: DublinCoreDatastream
  has_attributes :title, :created, datastream: 'descMetadata', multiple: false
end

Now let's add a created date:

asset = MyObj.new.descMetadata
=> #<DublinCoreDatastream @pid="" @dsid="descMetadata" @controlGroup="M" changed="false" @mimeType="text/plain" >
>> asset.created = Date.parse('1952-02-23')
=> Sat, 23 Feb 1952
>> asset.created.first.class
=> Date
>> puts asset.graph.dump(:ntriples)
<http://example.com/1234> <http://purl.org/dc/terms/created> "1952-02-23Z"^^<http://www.w3.org/2001/XMLSchema#date> .
>> puts asset.to_solr
{"desc_metadata__created_dtsim"=>["1952-02-23T00:00:00Z"]}

As you see, the created attribute is in solr with a suffix of _dtsim, where the dt represents date, and not a string literal.

Next Step

Go on to Lesson: Using RDF Lists or return to the Tame your RDF Metadata with ActiveFedora landing page.

Clone this wiki locally