Ruby Datamapper Will Not Load

ruby datamapper will not load

You need to require 'data_mapper', not datamapper.

Note there is a datamapper gem as well as a data_mapper gem, but they are the same thing, just different names. You need use data_mapper as the library name in both of them.

As far as I can tell datamapper is a straight copy of data_mapper:

$ diff -r data_mapper-1.2.0/ datamapper-1.2.0/
diff -r data_mapper-1.2.0/Rakefile datamapper-1.2.0/Rakefile
21c21
< GEM_NAME = 'data_mapper'
---
> GEM_NAME = 'datamapper'

Why datamapper methods doesn't work?

new_consultation.save checks to see if your object is valid before saving. If the object is valid, it will save and return true; otherwise, it will not save and return `false.

It looks like your object is invalid because you've specified:

property :refund, Text, required: true 

If refund is required, you need to specify it or your object won't save.

Note that puts new_consultation.date works because date is still stored in the local object, even when save fails and none of the data is saved to the database.

Ruby Datamapper will not insert serial data

Thanks to Zoltan for putting me on the right track. I added .chomp to sp.gets to remove the new line that sp.gets does. This allowed the database insert to occur.

Having done that, I still require the new line for the printf and file insert so will need to alter some code. Not expecting too many issues with this.

Issue with Data Mapper And Sinatra in Ruby 1.9.3

You need to require data_mapper, not datamapper (note the underscore):

require 'data_mapper'

See the DataMapper getting started page.

Adapters not working with datamapper

Uninstalling dm-postgres-adapter 1.2.0.rc2 fixed it.

ruby data mapper, load and object and associated objects

Unfortunately, there is not a good way to do that. The easiest solution is to actually query the individual records, which is of course even slower than loading all of the attributes at once with something like:

objects = ModelObject.all.map { |o| ModelObject.get(o.id) }

Slightly more complex, you could overload DataMapper::Resource with a method like this:

  def from_sql(sql, *bind_values)
self.repository.adapter.select(sql, *bind_values).map(&:as_json).map do |h|
if h.is_a?(Hash)
self.new(h)
else
self.new(id: h)
end.tap do |record|
record.persistence_state = DataMapper::Resource::PersistenceState::Clean.new(record)
end
end
end

and then pass in something like this:

properties = ModelObject.properties.map(&:field)
objects = ModelObject.from_sql("SELECT #{properties.join(", ")} FROM table_name")

Datamapper Update does not update record

Removing attr_accessor fixed the problem. From my research attr_accessor is used for attributes not in the database.



Related Topics



Leave a reply



Submit