Skip to content

Lesson: using typed predicates in your models

Justin Coyne edited this page Jan 20, 2014 · 6 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
  map_predicates do |map|
    map.title(in: RDF::DC)
    map.created(in: RDF::DC) do |index|
      index.as :stored_searchable
      index.type :date
    end
    #...
  end
end
class MyObj < ActiveFedora::Base
  has_metadata 'descMetadata', type: DublinCoreDatastream
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