Data-Mapper Under passenger and ruby 1.8 - ruby

My sinatra app is showing an error when declaring required on fields using datamapper running on passenger and ruby 1.8
error: undefined local variable or method `required' for Person:Class
class Person
include DataMapper::Resource
property :id, Serial
property :salutation, String
property :first_name, String , required => true
property :last_name, String , required => true
property :email, String , required => true, :format => :email_address
property :phone, String , required => true
property :dob, String
property :no_of_guests, String , required => true
property :attending, String, required => true
property :created_at, DateTime
end
Is this an issue with datamapper and ruby 1.8, or passenger or the way I'm decalring the required attribute?

requiredhas to be a symbol (:required):
class Person
include DataMapper::Resource
property :id, Serial
property :salutation, String
property :first_name, String , :required => true
property :last_name, String , :required => true
property :email, String , :required => true, :format => :email_address
property :phone, String , :required => true
property :dob, String
property :no_of_guests, String , :required => true
property :attending, String, :required => true
property :created_at, DateTime
end

Related

DataMapper Associations

I was just trying to create a database with DataMapper. I am trying to associate the class User with class Post, Post2, Post3 and Post4. And I didnt get any message or changes in the Data SQLite Free, a database Mac application that I am using. Below is the code I created. Did I make any mistakes? Thanks!
require 'sinatra'
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/wiki2.db")
class User
include DataMapper::Resource
property :id, Serial
property :username, Text, :required => true
property :password, Text, :required => true
property :date_joined, DateTime
property :edit, Boolean, :required => true, :default => false
has n, :posts
has n, :post2s
has n, :post3s
has n, :post4s
end
class Post
include DataMapper::Resource
property :id, Serial
property :content, Text
property :created_at, DateTime
property :updated_at, DateTime
belongs_to :user
end
class Post2
include DataMapper::Resource
property :id, Serial
property :content, Text
property :created_at, DateTime
property :updated_at, DateTime
belongs_to :user
end
class Post3
include DataMapper::Resource
property :id, Serial
property :content, Text
property :created_at, DateTime
property :updated_at, DateTime
belongs_to :user
end
class Post4
include DataMapper::Resource
property :id, Serial
property :content, Text
property :created_at, DateTime
property :updated_at, DateTime
belongs_to :user
end
DataMapper.finalize.auto_upgrade!

Not null fields for ActiveAttr

I'm having issues enforcing a field to not be nil within ActiveAttr::Model.
Is there an elegant way of enforcing this constraint within the model instead of defining it in the controller? Or am I testing incorrectly for the scenario?
Model:
class Message
include ActiveAttr::Model
attribute :name, :presence => true, :allow_nil => false, :allow_blank => false
attribute :email, :presence => true
attribute :content, :presence => true
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :content, :maximum => 500
end
RSpec:
module MessageSpecHelper
def valid_message_attributes
{
:name => "Walter White",
:email => "walter#hailtheking.com",
:content => "I am the one who knocks"
}
end
end
it "should have error on name (alternate with nil)" do
#message.attributes = valid_message_attributes.except(:name => nil)
#message.should have(1).error_on(:name)
end

Don't work behaviors in Datamapper

I work with Sinatra. This is my models.
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :posted, Boolean, :default => true
has n, :comments
has n, :tags
end
class Comment
include DataMapper::Resource
property :id, Serial
property :user, String
property :body, Text
property :posted, Boolean, :default => false
belongs_to :post
end
class Tag
include DataMapper::Resource
property :id, Serial
property :tag, String
property :weight, Integer, :default => 1
belongs_to :post
end
Create a post
tags = params[:tags].split(' ')
post = Post.new(:title=>params[:title],:body=>params[:body])
tags.each { |tg|
post.tags << Tag.create(:tag=>tg)
}
redirect '/admin' if post.save
But no tags. What do I need to fix?
If you use one-to-many relation, you should create tags with :post set to post:
tags.each { |tg|
Tag.create(:tag => tg, :post => post)
}

Cannot modify data with Mongoid

I can read my database using Mongoid, but cannot write to it.
This example below successfully outputs the activity's device type, but it crashes on the save method with this error message: "undefined method `name' for nil:NilClass"
activity = Activity.find('4d89568d4f21557eb10003fc')
puts activity.deviceType
activity.description = 'my description'
activity.save
Here are my class definitions:
class User
include Mongoid::Document
field :name, :type => String
field :identifier, :type => String
field :email, :type => String
referenced_in :activity
end
class Trackpoint
include Mongoid::Document
field :when, :type => DateTime
field :latitude, :type => Float
field :longitude, :type => Float
field :distance, :type => Float
field :altitude, :type => Float
field :heartRate, :type => Integer
embedded_in :activity, :inverse_of => :trackpoint
end
class Activity
include Mongoid::Document
field :startTime, :type => DateTime
field :description, :type => String
field :sport, :type => String
field :deviceType, :type => String
field :deviceId, :type => String
field :deviceActivityId, :type => String
field :isPublic, :type => Boolean
field :user_id, :type => String
embeds_many :trackpoints
references_one :user
end
Thanks for any help...
Just got rid of the :inverse_of statements and it works now!

Validate uniqueness on combined fields in DataMapper

I want to be able to put entries in my database where the manufacturer will be represented multiple times but not the same combination of manufacturer and model.
So "Sony(manufacturer), Tv(model)" is okay "Sony(manufacturer), OtherTv(model)" but the third entry "Sony(manufacturer), Tv(model)" is not okay since the combination of manufacturer and model is not unique. I tried with the :key => true validation but it doesn't seem to work. And I cannot do something like validates_uniqueness_of :manufacturer AND :model I guess. So how do you do it?
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :key => true
property :model, String, :key => true
validates_uniqueness_of :
end
You can actually go with:
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :unique_index => :manufacturer_model
property :model, String, :unique_index => :manufacturer_model
validates_uniqueness_of :model, :scope => :manufacturer
end
This will also give you a unique index in the database.
Nevermind.
It seems this did the job:
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :unique_index => true
property :model, String, :unique_index => true
validates_uniqueness_of :model, :scope => :manufacturer
end
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :unique_index => :manufacturer_model
property :model, String, :unique_index => :manufacturer_model
validates_is_unique :model, :scope => :manufacturer
end
I needed to modify the example to use "validates_is_unique" to get this to work.
If you remove the property :id Serial line, you will end up with a composite key (Manufacturer, Model), that will not allow duplicate Models for a particular Manufacturer.

Resources