has n of Strings in DataMapper - ruby

I'm writing a client for a web service, and part of their model is a list of strings belonging to a user containing previous usernames the user went by. I'm trying to use DataMapper for my client gem, and my first thought was to use DataMapper's has n syntax, but I can't seem to apply this to Strings. Is there a better way of doing this perhaps?
My current code:
class User
include DataMapper::Resource
# Some Properties here
has n, :also_known_as, 'String'
end
The error this generates is this:
irb(main):001:0> require 'cloudsdale'
NoMethodError: undefined method `relationships' for String:Class
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/associations/one_to_many.rb:109:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `block in finalize_relationships'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:782:in `finalize_relationships'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model.rb:137:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `block in finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:64:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `block in each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/ordered_set.rb:319:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/subject_set.rb:210:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/descendant_set.rb:63:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core.rb:281:in `finalize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/cloudsdale-0.0.1/lib/cloudsdale.rb:19:in `<top (required)>'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'irb(main):002:0>
The file that the error spawns from is this:
# Load in DataMapper
# Change the setup to meet your DB needs
require 'data_mapper'
DataMapper.setup(:default, 'abstract::')
# API objects
require 'cloudsdale/version'
require 'cloudsdale/api/api'
require 'cloudsdale/client'
# Models
require 'cloudsdale/models/user'
require 'cloudsdale/models/cloud'
require 'cloudsdale/models/avatar'
require 'cloudsdale/models/ban'
require 'cloudsdale/models/chat'
# Finalize DataMapper so the models Load
DataMapper.finalize

why do you use 'String' here?
has n, :also_known_as, 'String'
it make no sense, remove it:
has n, :also_known_as
if you want to set model, use :model option:
has n, :also_known_as, :model => ModelName
and i'm not sure you want to use String as model name.
most likely you need an extra model to keep user's previous names:
class UserAlias
include DataMapper::Resource
# ...
end
class User
include DataMapper::Resource
# ...
has n, :also_known_as, :model => UserAlias
end

If you want to be able to search for older usernames via the datamapper 1.x query DSL you need to define an additional model.
class User
include DataMapper::Resource
# ...
has n, :also_known_as, :model => UserNameHistory
end
class UsernameHistory
include DataMapper::Resource
property :id, Serial
property :name
belongs_to :user
end
If you do not need to query via the old usernames you can use a serialized large object. As a suggestion you could use the DataMapper::Property::YAML from dm-types like this:
class User
include DataMapper::Resource
# ...
property :also_known_as, YAML
end

Related

ArgumentError when accessing pageobject widget

class MainTabsWidget < PageObject::Elements::UnorderedList
include PageObject
include PageObject::PageFactory
link(:first_link, :text => 'First')
link(:second_link, :text => 'Second')
link(:third_link, :text => 'Third')
link(:category_link, :text => 'Category')
link(:subcat1_link, :text => 'Subcat1')
link(:subcat2_link, :text => 'Subcat2')
def goto_tab_item(tab_item)
items = tab_item.split /\//
items.each do |item|
case item
when 'First'
first_link
when 'Second'
second_link
when 'Third'
third_link
when 'Category'
category_link
when 'Subcat1'
subcat1_link
when 'Subcat2'
subcat2_link
end
end
end
end
PageObject.register_widget :main_menu, MainTabsWidget , :ul
I have a page called landing page that uses the widget in the following manner....
class LandingPage
include PageObject
include PageObject::PageFactory
main_menu(:menu_tabs, :id => 'mainMenu')
def select_menu_item(item)
menu_tabs_element.go_to_tab_item item
end
end
This actually did work at one point. If you pass 'First' for select_menu_item, it will click to the page specified by first_link. If you specify 'Category/Subcat1' it will click on the category link and then subcat1.
Howerver, something changed. We are transition from watir-webdriver gem to watir (6.10) gem, and now the code gets the argument exeption
'ArgumentError: wrong number of arguments (0 for 1..2)'
This occurs when menu_tabs_element gets referenced in any way. I've run out of ideas to remedy this.
Stack Trace:
ArgumentError: wrong number of arguments (0 for 1..2)
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/element_locators.rb:10:in `element'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/elements/element.rb:191:in `respond_to_missing?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:53:in `respond_to_missing?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:70:in `respond_to?'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object.rb:70:in `initialize'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1047:in `new'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1047:in `find_watir_element'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/platforms/watir/page_object.rb:1118:in `block in define_widget_singular_accessor'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page-object-2.2.4/lib/page-object/widgets.rb:38:in `block (3 levels) in define_accessors'
./lib/pages/menu.rb:13:in `select_menu_item'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:134:in `block in navigate_through_pages'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:129:in `each'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:129:in `navigate_through_pages'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/page_navigation-0.10/lib/page_navigation.rb:67:in `navigate_to'
./spec/refactor/test_spec.rb:6:in `block (2 levels) in <top (required)>'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `instance_exec'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:509:in `block in run_owned_hooks_for'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `each'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `run_owned_hooks_for'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:460:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:537:in `block in run_before_context_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:186:in `block in isolate_for_context_hook'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:182:in `instance_exec'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/memoized_helpers.rb:182:in `isolate_for_context_hook'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:536:in `run_before_context_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:589:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `map'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1896:in `with_suite_hooks'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/reporter.rb:79:in `report'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:112:in `run_specs'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:87:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
C:/Ruby22/bin/rspec:23:in `load'
C:/Ruby22/bin/rspec:23:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
1 example, 1 failure, 0 passed
Finished in 6.087 seconds
Process finished with exit code 1
It is not clear to me which change in Watir would cause this issue. It seems more likely a change in Page-Object. At any rate, here are 2 solutions. Note that this was tested with Page-Object v 2.2.4.
Using Widgets
As pointed out by Alexis in Page-Object Issue 263, including page object accessor methods in widgets is not as straight forward as one would expect. In the MainTabsWidget class, instead of include PageObject, you need to do:
extend PageObject::Accessors # to get the accessor methods
attr_reader :platform # the accessor methods don't work without a platform
With this change, your page object should work.
Using Page Section
Page-Object has a page section feature that might better suit your needs. It simplifies some of the code (ie address the above issue).
You define a class to represent your main tabs section. Note that you do not need to register the section or have the multi-line setup as with Widgets.
class MainTabs
include PageObject
include PageObject::PageFactory
link(:first_link, :text => 'First')
link(:second_link, :text => 'Second')
link(:third_link, :text => 'Third')
link(:category_link, :text => 'Category')
link(:subcat1_link, :text => 'Subcat1')
link(:subcat2_link, :text => 'Subcat2')
def select_menu_item(tab_item)
items = tab_item.split /\//
items.each do |item|
case item
when 'First'
first_link
when 'Second'
second_link
when 'Third'
third_link
when 'Category'
category_link
when 'Subcat1'
subcat1_link
when 'Subcat2'
subcat2_link
end
end
end
end
The page object can simply become:
class LandingPage
include PageObject
include PageObject::PageFactory
page_section(:menu_tabs, MainTabs, :id => 'mainMenu')
end
The clicking of the menu would change to:
page.menu_tabs.select_menu_item('First')
(or you could continue to define a #select_menu_item method in LandingPage to avoid downstream changes)

RSpec with FactoryGirl/Bot - undefined method 'user' on model that belongs to user through has_many :through

Trying to get into proper testing and figure out the ins and outs of basic RSpec with FactoryBot.
NOTE: The validation tests all passed previously and they're in the model files. I've just removed them for the sake of reducing clutter.
My models:
models/user.rb
has_many :fulfillments
has_many :milestones, through: :fulfillments
models/fulfillment.rb
has_many :milestones
belongs_to :user
models/milestone.rb
belongs_to :fulfillments
My Factories:
spec/factories/users.rb
require 'faker'
FactoryBot.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
preferred_name Faker::Name.first_name
username Faker::Internet.user_name
email Faker::Internet.email
password 'password123'
password_confirmation 'password123'
factory :user_with_fulfillments do
transient do
fulfillments_count 3
end
after(:create) do |user, e|
create_list(:fulfillment_with_milestones, e.fulfillments_count, user: user)
end
end
end
end
spec/factories/fulfillments.rb
require 'faker'
FactoryBot.define do
factory :fulfillment do
title Faker::Lorem.words.join(' ')
description Faker::Lorem.sentences.join(' ')
promise Faker::Lorem.sentence
reason Faker::Lorem.sentence
association :user, factory: :user_with_fulfillments
trait :userless do
user nil
end
factory :fulfillment_with_milestones do
transient do
milestones_count 2
end
after(:create) do |fulfillment, e|
create_list(:milestone, e.milestones_count, fulfillment: fulfillment)
end
end
end
end
spec/factories/milestones.rb
require 'faker'
FactoryBot.define do
factory :milestone do
title Faker::Lorem.words.join(' ')
criteria Faker::Lorem.sentences.join("\n")
reason Faker::Lorem.sentence
deadline Faker::Date.forward(30)
association :fulfillment, factory: :fulfillment_with_milestones
end
end
One test that's giving me trouble (spec/models/milestone.rb)
require 'rails_helper'
RSpec.describe Milestone, type: :model do
it 'has a valid factory' do
expect(create(:milestone)).to be_valid
end
it 'validates attributes correctly' do
should validate_presence_of :fulfillment
should validate_presence_of :title
should validate_presence_of :criteria
end
end
Error I'm getting in ANY test that involves the milestones factory
Failures:
1) Fulfillment has a valid factory
Failure/Error: create_list(:milestone, e.milestones_count, fulfillment: fulfillment)
NoMethodError:
undefined method `user' for #<Milestone:0x00007f844d54f1b8>
# ./spec/factories/fulfillments.rb:22:in `block (4 levels) in <top (required)>'
# ./spec/factories/users.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/models/fulfillment_spec.rb:6:in `block (2 levels) in <top (required)>'
2) Milestone has a valid factory
Failure/Error: expect(create(:milestone)).to be_valid
NoMethodError:
undefined method `user' for #<Milestone:0x00007f844d8d0b98>
# ./spec/models/milestone_spec.rb:6:in `block (2 levels) in <top (required)>'
3) Milestone validates attributes correctly
Failure/Error: should validate_presence_of :fulfillment
NoMethodError:
undefined method `user' for #<Milestone:0x00007f8450829700>
# ./spec/models/milestone_spec.rb:10:in `block (2 levels) in <top (required)>'
4) User can have fulfillments and milestones
Failure/Error: create_list(:milestone, e.milestones_count, fulfillment: fulfillment)
NoMethodError:
undefined method `user' for #<Milestone:0x00007f8450b7b9d0>
# ./spec/factories/fulfillments.rb:22:in `block (4 levels) in <top (required)>'
# ./spec/factories/users.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:29:in `block (2 levels) in <top (required)>'
Finished in 0.26253 seconds (files took 5.19 seconds to load)
9 examples, 4 failures, 1 pending
Failed examples:
rspec ./spec/models/fulfillment_spec.rb:5 # Fulfillment has a valid factory
rspec ./spec/models/milestone_spec.rb:5 # Milestone has a valid factory
rspec ./spec/models/milestone_spec.rb:9 # Milestone validates attributes correctly
rspec ./spec/models/user_spec.rb:28 # User can have fulfillments and milestones
Mostly confused because I'm not sure where or why it's trying to call #user on an instance of Milestone. The factories create Fulfillments for a User, then Milestones for the Fulfillments.
PS: It's my first time posting to StackOverflow (I usually manage to find answers to my really simple questions) so feel free to let me know if there's anything I could do to make future questions clearer.
It turns out I'm an idiot. I had a
validates :user, :presence
on my Milestone model. No wonder why it was trying to call milestone.user
Forget this ever happened please. ]:
Try to define classes explicitly in your factories so that they are not guessed but known.
factory :user, class: User
and
factory :fulfillment, class: FulFillment

Circular require issue with Ruby

I am trying to implement a currency based application using Ruby, I find that:
require 'dollar'
require 'franc'
puts 'Why does this not work?'
class Money
attr_reader :amount
def self.dollar(number)
Dollar.new number
end
def ==(other)
self.amount == other.amount
end
def self.franc(number)
Franc.new(number)
end
end
I have the Franc class that looks like this:
require 'money'
class Franc < Money
attr_reader :amount
def initialize(amount)
#amount = number
end
def times(mul)
amount = #amount * mul
Franc.new(amount)
end
def ==(other)
return false unless other.is_a? self.class
super
end
end
This is a direct translation of some of the code from the Kent Beck book to Ruby. When I run bin/rspec I see:
/home/vamsi/Do/wycash/lib/franc.rb:3:in `<top (required)>': uninitialized constant Money (NameError)
from /home/vamsi/Do/wycash/lib/money.rb:2:in `require'
from /home/vamsi/Do/wycash/lib/money.rb:2:in `<top (required)>'
from /home/vamsi/Do/wycash/lib/dollar.rb:1:in `require'
from /home/vamsi/Do/wycash/lib/dollar.rb:1:in `<top (required)>'
from /home/vamsi/Do/wycash/spec/dollar_spec.rb:1:in `require'
from /home/vamsi/Do/wycash/spec/dollar_spec.rb:1:in `<top (required)>'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>'
from bin/rspec:17:in `load'
from bin/rspec:17:in `<main>'
I was going to add this as a comment when I realised it would start getting too lengthy.
When you say you have translated directly from Kent Beck's book I assume you are referring to his TDD by Example book (it's the only book I can find of his that references the currency example). I can't actually find an example in that book however that refers to cyclic dependancy, however from previous experience with Java and C++ you normally try to break up a cyclic dependancy by implementing an interface - for Ruby, you might want to refer to the following SO question which has good answers to this problem:
Circular Dependancies in Ruby
Having said that I think your design is broken. Your Money class should be defining the generic behaviour and attributes for all Money types. It should know nothing about Franc's or Dollars, etc... Specific behaviour for Franc should be encapsulated entirely within the Franc class. Either use a single class to handle all the currencies or use inheritance - it doesn't make much sense to do both.
You should put require 'franc' after you defined Money in your first script.
The class Money will be then defined when Ruby executes your second_script.
EDIT:
Circular require isn't an issue :
# a.rb
puts "A"
require_relative 'b'
# b.rb
puts "B"
require_relative 'a'
# ruby a.rb
A
B
A
Replacing require_relative with load './b.rb' would result in an infinite loop and "stack level too deep" Error.
Still, you should probably define Money in money.rb, Franc in franc.rb, ... and not put anything about Franc in Money.

Association for table less model

I've been trying to implement the association at FrontEnd but as currently the application in not having any database directly connected with the website, so as a result we can not use the ActiveRecord and only using the ActiveModel for supporting the validations and core features of a Model. Now as we need to use the nested attributes which we are going to send along with an object, the addresses which are associated with the User, so for this we need to first define the association on the corresponding model. But after defining the association it is throwing exception of undefined method "has_many" on User model. I'm currently searching the way to implement it in our website and implement the logic of nested attributes.
It would be great if you can suggest me anything related to this or if you have met with such issue in the past.
I've also tried the approach using the gem https://github.com/softace/activerecord-tableless but not working for me. Also I've added a tableless.rb
tableless.rb
class Tableless < ActiveRecord::Base
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null )
end
def self.columns()
#columns ||= [];
end
def self.columns_hash
h = {}
for c in self.columns
h[c.name] = c
end
return h
end
def self.column_defaults
Hash[self.columns.map{ |col|
[col.name, col.default]
}]
end
def self.descends_from_active_record?
return true
end
def persisted?
return false
end
# override the save method to prevent exceptions
end
But getting the following exception Exception:
Console Error:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:203:in `table_exists?'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:92:in `get_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:77:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:79:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:32:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:70:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:19:in `__temp__9646='
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:13:in `block in initialize'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `each'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `initialize'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from (irb):19
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
It seems that you forgot to call the method
has_no_table
On your model, as per https://github.com/softace/activerecord-tableless#usage. In their example:
class ContactMessage < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
validates_presence_of :name, :email
end
Hope this helps. =)

Problems with MongoID and one to many relations

I encountered the following problem with MongoID.
I've created two models which related by one to many.
require 'mongoid'
class User
include Mongoid::Document
has_many :configs
field :login, :type => String, unique: true
field :password, :type => String
field :email, :type => String
end
class Config
include Mongoid::Document
belongs_to :user
field :links, :type => Array
field :root, :type => Array
field :objects, :type => Array
field :categories, :type => Array
end
After that I've created new User instance:
user = User.new
user.login = "login1"
user.password = "password1"
user.email = "email1"
user.save
But I saw the following output:
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:229: Use RbConfig instead of obsolete and deprecated Config.
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/referenced/many.rb:550:in `criteria': undefined method `where' for RbConfig:Module (NoMethodError)
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/metadata.rb:143:in `criteria'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/builders/referenced/many.rb:20:in `build'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:43:in `create_relation'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:26:in `build'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:101:in `block (4 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:125:in `_loading'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:100:in `block (3 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:99:in `block (2 levels) in getter'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:51:in `read_attribute_for_validation'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:151:in `block in validate'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `each'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `validate'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:310:in `_callback_before_1'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:407:in `_run__852778899__validate__1039643020__callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:212:in `run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:403:in `_run__852778899__validation__1039643020__callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:179:in `valid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:75:in `valid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:187:in `invalid?'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:23:in `block in prepare'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `tap'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `prepare'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:49:in `insert'
from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:154:in `upsert'
from parser/parsing_starter.rb:47:in `<main>'
If I remove the lines with "has_many" and "belongs_to" then the program will work correctly.
Can anyone tell what am I doing wrong?
Looks like there's some kind of collision with your Config class and one that might already be defined. Try changing the name of the model to something like UserConfig.
Or you can use a wrapper module to namespace your classes:
module MyApp
class Config
...
end
class User
...
end
end

Resources