Strange problem with factory girl - ruby

I have a model
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# name :string(255)
# title :string(255)
# content :text
# created_at :datetime
# updated_at :datetime
# abstract :text
# category_id :integer
# finalversion :boolean default(FALSE)
# published_date :date
#
class Post < ActiveRecord::Base
has_many :tags
belongs_to :category
validates :published_date, :presence => true
default_scope :order => 'created_at DESC'
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
def prev_post
self.class.first(:conditions => ["id < ?", id], :order => "id desc")
end
def next_post
self.class.first(:conditions => ["id > ?", id], :order => "id asc")
end
def seo_title
title.gsub(/\s+/,'_')
end
end
and a factory
FactoryGirl.define do
factory :post do
published_date Date.today
association :category, :factory => :category
title Forgery::LoremIpsum.words
name Forgery::LoremIpsum.word
content Forgery::LoremIpsum.words(100, :random => 250)
abstract Forgery::LoremIpsum.words(100, :random => 50)
finalversion true
end
end
and in the rails console I have no problem doing
FactoryGirl.create :post
to get a valid object and am able to access the *published_date* attribute. However in my spec
require 'spec_helper'
(1..5).map do |i|
title = "a title\t#{i}"
escape_title = "a_title_#{i}"
perma_link = "/posts/#{i}/title/#{escape_title}"
describe "A post with title '#{title}'" do
before do
#post = FactoryGirl.create :post, :id=>i, :title => title
visit '/posts'
end
it "should appear in all links with permalink #{perma_link}" do
within "section.post_#{#post.id}" do
page.should have_xpath(%Q%.//h1/a[#href="#{perma_link}"]%)
within "div.teaser" do
page.should have_xpath(%Q%.//a[#href="#{perma_link}"]%)
end
end
end
end
end
I get the backtrace error
5) A post with title 'a title 5' should appear in all links with permalink /posts/5/title/a_title_5
Failure/Error: #post = FactoryGirl.create :post, :id=>i, :title => title
NoMethodError:
undefined method `published_date=' for #<Post:0x000001047266b8>
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:364:in `method_missing'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:46:in `method_missing'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/proxy/build.rb:13:in `set'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/attribute/static.rb:12:in `add_to'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:93:in `block in run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `each'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/factory.rb:91:in `run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/bundler/gems/factory_girl-4f5f5df39a1b/lib/factory_girl/syntax/methods.rb:54:in `create'
# ./spec/integration/posts_permalinks_spec.rb:12:in `block (3 levels) in <top (required)>'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `run_in'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `block in run_all'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `each'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:110:in `run_hook'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `each'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:144:in `run_before_each'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:48:in `block (2 levels) in run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `block in run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `block in with_pending_capture'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `block in run_examples'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `run_examples'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:232:in `run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `map'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `block in run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/reporter.rb:12:in `report'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:24:in `run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
# /Users/bradphelan/.rvm/gems/ruby-1.9.2-p180#ingd/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'
I'm totally stumped on it. Any ideas
Brad

Did you run rake db:test:prepare? It looks to me like your Post model has published_date, but your Factory is saying it doesn't exist. Not running this rake task would be the major thing that would cause this.

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)

Factory Girl + Rspec: ArgumentError: wrong number of arguments (0 for 2)

I have a simple restaurant class that looks like this:
module Restaurant
class Identity
attr_reader :name, :location
def initialize (name, location)
#name = name
#location = location
end
end
end
My factory looks like this:
FactoryGirl.define do
factory :restaurant, :class => Restaurant::Identity do |f|
f.name "Alfredos"
f.location "Andheri"
end
end
And my test is written like this:
describe Restaurant::Identity do
subject { build(:restaurant) }
its(:name) {should_not be_nil}
its(:location) {should_not be_nil}
end
But when I run this, I get
1) Restaurant::Identity name
Failure/Error: subject { build(:restaurant) }
ArgumentError:
wrong number of arguments (0 for 2)
# ./lib/restaurant.rb:7:in `initialize'
# ./spec/restaurant_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/restaurant_spec.rb:11:in `block (2 levels) in <top (required)>'
Why is this happening? What am I doing wrong?
Ok so the solution is to use initialize_with in your factory girl setup:
FactoryGirl.define do
factory :restaurant, :class => Restaurant::Identity do |f|
f.name "Alfredos"
f.location "Andheri"
initialize_with { new(name, location) } # add this line
end
end
ref: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#custom-construction

Rails 3 with composed_of model and validation get error "NoMethodError: undefined method `marked_for_destruction?'"

I have tried to add custom validation as is written in Rails 3 with composed_of model and validation but i got strange error: "NoMethodError: undefined method `marked_for_destruction?' for Money"
And I don't understand what is wrong.
Can you help me?
Model:
# == Schema Information
#
# Table name: transactions
#
# id :integer not null, primary key
# text :string(255)
# amount_cents :integer default(0), not null
# ...
class Transaction < ActiveRecord::Base
attr_accessible :text, :amount, ...
validates :text, :length => { :maximum => 255 }
composed_of :amount, :class_name => "Money", :mapping => %w(amount_cents cents),
:converter => Proc.new { |value| Money.to_money(value) }
validates :amount, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates_associated :amount
...
end
class Money
attr_reader :cents
def initialize(cents)
#cents = cents
end
class << self
def to_money(str_money)
cents = (str_money.to_f * 100).to_i
Money.new(cents)
end
def to_money?(str_money)
/\A\d+(\.\d+)?\z/ == str_money.to_s
end
end
def ==(value)
#cents == self.class.to_money(value).cents
end
def to_i
#cents
end
def to_f
#cents.to_f
end
def to_s
return nil if #cents.nil?
unit, subunit = #cents.abs.divmod(100)
unit_str = ""
subunit_str = ""
fraction_str = ""
unit_str, subunit_str = unit.to_s, subunit.to_s
subunit_str.insert(0, '0') while subunit_str.length < 2
absolute_str = "#{unit_str}.#{subunit_str}#{fraction_str}"
absolute_str.tap do |str|
str.insert(0, "-") if #cents < 0
end
end
def inspect
"#<Money cents:#{#cents} to_s:#{self.to_s}>"
end
end
Error:
1.9.3p194 :001 > t = Transaction.new
=> #<Transaction id: nil, text: nil, amount_cents: 0, date: nil, created_at: nil, updated_at: nil, transaction_type_id: nil, account_id: nil, user_id: nil, trans_account_id: nil, trans_amount_cents: 0>
1.9.3p194 :002 > t.amount = 100
=> 100
1.9.3p194 :003 > t.valid?
NoMethodError: undefined method `marked_for_destruction?' for #<Money cents:10000 to_s:100.00>
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `block in validate_each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `reject'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `validate_each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:153:in `block in validate'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `validate'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:310:in `_callback_before_21'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:462:in `_run__74709952__validate__911291598__callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:227:in `run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:403:in `_run__74709952__validation__911291598__callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:194:in `valid?'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations.rb:69:in `valid?'
from (irb):3
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

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

Having trouble with Nokogiri and Rails

Been at this for a while. If i tell Category to just create, everything works fine. If I tell it to find_or_create I get errors.
These work:
puts topic.at_xpath("#topicid")
puts topic.at_xpath("#topicname")
and
Category.create!(:topic_id => topic.at_xpath("#topicid"), :name => topic.at_xpath("#topicname"))
But these don't:
Category.find_by_name(topic.at_xpath("#topicname"))
or
Category.find_or_create_by_topic_id_and_name(topic.at_xpath("#topicid"), topic.at_xpath("#topicname"))
Where am I messing up?
class FeedEntry < ActiveRecord::Base
require 'nokogiri'
require 'open-uri'
has_many :category_feeds
has_many :categories, :through => :category_feeds
accepts_nested_attributes_for :categories, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? }
def self.nokogiri_get_feed
url = "http://some_feed.com/atom_feed"
doc = Nokogiri::HTML(open(url))
doc.remove_namespaces!
doc.search('feed entry').each do |item|
unless exists? :guid => item.css('id').text
create!(:name => item.css('title').text, :summary => item.css('title').text, :url => item.at_css("link")[:href], :published_at => item.css('updated').text, :guid => item.css('id').text)
item.xpath('content').each do |i|
i.css('topic').each do |topic|
id = topic.at_xpath("#topicid")
name = topic.at_xpath("#topicname")
update_attributes!(:categories=>[Category.find_or_create_by_topic_id_and_name(id, name)])
end
end
end
end
end
end
errors are:
ruby-1.9.2-p180 :001 > FeedEntry.nokogiri_get_feed
TypeError: Cannot visit Nokogiri::XML::Attr
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:21:in `rescue in visit'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `visit'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:260:in `visit_Arel_Nodes_Equality'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `visit'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:120:in `visit_Arel_Nodes_Grouping'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `visit'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:91:in `block in visit_Arel_Nodes_SelectCore'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:91:in `map'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:91:in `visit_Arel_Nodes_SelectCore'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:77:in `block in visit_Arel_Nodes_SelectStatement'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:77:in `map'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:77:in `visit_Arel_Nodes_SelectStatement'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/sqlite.rb:7:in `visit_Arel_Nodes_SelectStatement'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:15:in `visit'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/visitor.rb:5:in `accept'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib/arel/visitors/to_sql.rb:19:in `block in accept' ... 11 levels...
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:238:in `each'
from /Users/pca/projects/cdapp/cdrails/app/models/feed_entry.rb:35:in `block (2 levels) in nokogiri_get_feed'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:238:in `each'
from /Users/pca/projects/cdapp/cdrails/app/models/feed_entry.rb:33:in `block in nokogiri_get_feed'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/xml/node_set.rb:238:in `each'
from /Users/pca/projects/cdapp/cdrails/app/models/feed_entry.rb:30:in `nokogiri_get_feed'
from (irb):1
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/pca/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Summary
Instead of these lines:
id = topic.at_xpath("#topicid")
name = topic.at_xpath("#topicname")
Use these instead:
id = topic['topicid']
name = topic['topicname']
Explanation
Let's look at a simple test case:
require 'nokogiri'
xml = Nokogiri::XML("<root foo='bar' />")
foo = xml.root.at_xpath('#foo')
puts foo
#=> bar
p foo
#=> #<Nokogiri::XML::Attr:0x15c1d64 name="foo" value="bar">
p foo.text
#=> "bar"
p xml.root['foo']
#=> "bar"
As you can see from the above, selecting an attribute via XPath actually gives you an Attr node, which is not the same as the string value of that attribute. (Using puts causes the to_s method of the Attr to show you only the value, but that doesn't mean that it's actually a string.)
As shown above, you need to use the text method (or value or content) on the Attr nodes to get the string value back that you really wanted:
id = topic.at_xpath("#topicid").text
name = topic.at_xpath("#topicname").text
Alternatively (and more simply) use the Element#[] method to fetch the value of an attribute off of an element directly:
id = topic['topicid']
name = topic['topicname']

Resources