Strange DataMapper (0.10.2) error. Please help! - ruby

See the full error here: http://notesapp.heroku.com/
I'm using DataMapper and dm-validations 0.10.2. No matter how much I tweak my models, I get the same error, or another one. Here's how my model looks like:
class User
include DataMapper::Resource
attr_accessor :password, :password_confirmation
property :id, Serial, :required => true
property :email, String, :required => true, :format => :email_address, :unique => true
property :hashed_password, String
property :salt, String, :required => true
property :created_at, DateTime, :default => Time.now
property :permission_level, Integer, :default => 1
validates_present :password_confirmation, :unless => Proc.new { |t| t.hashed_password }
validates_present :password, :unless => Proc.new { |t| t.hashed_password }
validates_is_confirmed :password

Looks like you have an old version of DataObjects (probably pre 0.10.0) installed. Please update to the latest version and I think this error will disappear. Depending on the database you use it's most likely either do_postgres or do_mysql you need to upgrade.

Related

Validation doesn't work in "sexy" style

It seems that Rails doens't let me pass in more than one parameter when using this validation syntax. It always has a syntax method for an unexpected comma after the first argument.
class Apartment < ActiveRecord::Base
geocoded_by :location
after_validation :geocode
has_many :image_attachments
validates_associated :image_attachments
accepts_nested_attributes_for :image_attachments
validates :location, presence: true
validates :description, presence: true
validates :price, :presence => true
,:format => { with: /^[0-9]+$/, message: 'must be a number' }
validates :longitude, presence: true
end
It's bad formatting (and very "unsexy") to have the comma at the beginning of the next line.
Better to do...
validates :price, :presence => true,
:format => { with: /^[0-9]+$/, message: 'must be a number' }
...which should work fine.
A more consistent style is to use the Ruby 1.9 convention for key/value when key is a symbol.
validates :price, presence: true,
format: { with: /^[0-9]+$/, message: 'must be a number' }

Show page, show associations

i try to find out how i can show associations deeper than one level.
Show at my FORM, i just done it there:
form do |f|
f.inputs "Details" do
f.input :name
f.input :item_category
f.input :resource
f.input :status
end
f.inputs "Actions" do
f.semantic_errors *f.object.errors.keys
f.has_many :item_actions, :allow_destroy => true, :heading => 'Planets', :new_record => true do |obj|
obj.input :action
obj.input :status
obj.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
obj.has_many :item_action_skills, :heading => "Skills" do |ias|
ias.input :skill
ias.input :level
end
end
end
f.actions
end
You can see, i show has_many :item_actions and going one level deeper to item_action.item_action_skills. On this form is works perfect.
Now i'll want it on the show page too. My code:
show do |obj|
attributes_table do
row :name
row :item_category
row(:resource) {|obj| status_tag((obj.resource ? 'yes' : 'no'), (obj.resource ? :ok : :error))}
row(:status) {|obj| status_tag(obj.status_string.first, obj.status_string.last) }
end
panel "Actions" do
table_for obj.item_actions do
column :action
column(:status) {|obj| status_tag(obj.status_string.first, obj.status_string.last) }
end
end
active_admin_comments
end
I write table_for, but how to go now to the next association?
I want the item_action.item_action_skills.
I have no idea. Any idea?
Thanks!
Ruby 1.9.3
Rails 3.2.14
ActiveAdmin 0.6.0
Try this:
panel "Actions" do
table_for obj.item_actions do
column :action
column(:status) {|obj| status_tag(obj.status_string.first, obj.status_string.last) }
column("skills"){|resource|
table_for resource.item_action_skills do
column(:your_column)
end
}
end
end

Mongomapper :minimum & :maximum vs. :length

I try to validate in MongoMapper a string with:
key :title, String, :require => true, :length => 4..30
And I got always the error "title is too short (minimum is 4 characters)" also when the string was longer than 4 chars.
If I try it with
key :title, String, :require => true, :minimum => 4, :maximum => 30
and it work as excepted. Can someone explain why this happen or why the first thing is wrong?
MongoMapper uses Activerecord validations. From their documentation: validates :password, :length => { :in => 6..20 } So you have to use :in to denote you are using a range. See http://guides.rubyonrails.org/active_record_validations_callbacks.html#length

DataMapper Redis: can't find child from parent, only parent from child

I'm using DataMapper with the redis adapter in a Ruby library.
I have these classes defined:
class Zone
include DataMapper::Resource
property :id, String, :key => true, :unique_index => true, :default => lambda { |x,y| UUID.new.generate }
property :preview_mode, Boolean, :default => false
timestamps :at
has 1, :campaign
end
and
class Campaign
include DataMapper::Resource
property :id, String, :key => true, :unique_index => true, :default => lambda { |x,y| UUID.new.generate }
property :name, String
timestamps :at
belongs_to :zone
has n, :rules
validates_presence_of :name
end
I'm able to do Campaign.first.zone but not Zone.first.campaign.
I would like to be able to do the lookups in both directions.
#lightyrs I responded to your github issue, but for reference here, I think this issue has been fixed in versions > 0.8 that have better support for non-serial primary keys.
Cheers! - whoahbot

Rails -- before_save not working?

I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email,: password, :password_confirmation
email_regex = /^[A-Za-z0-9._+-]+#[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/
#tests for valid email addresses.
validates :name, :presence => true,
:length => {:maximum => 50}
validates :email, :presence => true,
:format => {:with => email_regex},
:uniqueness => {:case_sensitive => false}
validates :password, :presence => true,
:length => {:maximum => 20, :minimum => 6},
:confirmation => true
before_save :encrypt_password
private
def encrypt_password
#encrypted_password = encrypt(password)
end
def encrypt(string)
string
end
end
(Obviously this isn't doing any encrypting because the encrypt method isn't really implemented but that's not my question)
I then wrote the following Spec (according to the tutorial):
require 'spec_helper'
describe User do
before(:each) do
#attr = { :name => "Example User", :email => "user#example.com",
:password => "abc123", :password_confirmation => "abc123"}
end
describe "password encryption" do
before(:each) do
#user = User.create!(#attr) # we are going to need a valid user in order
# for these tests to run.
end
it "should have an encrypted password attribute" do
#user.should respond_to(:encrypted_password)
end
it "should set the encrypted password upon user creation" do
#user.encrypted_password.should_not be_blank
end
end
end
The first of these tests passes, but since #user.encrypted_password is nil, the second test fails. But I don't understand why it's nil since the encrypt_password method should be being called by before_save. I know I must be missing something -- can someone please explain?
The encrypt_password method is incorrect, it should read:
def encrypt_password
self.encrypted_password = encrypt(password)
end
Note the use of self, which will properly set the attribute for the user object rather than creating an instance variable which is forgotten.
This is an old question and this is more of a comment but I don't have enough reputation to comment yet. Just wanted to link this question too as it goes into some solid detail about self.
Why isn't self always needed in ruby / rails / activerecord?

Resources