How to use rails api find_or_create_by in ruby? - ruby-on-rails

I tried to run test a gem for rails.
In the test, I am in ruby not rails.
The purpose of this code is to create rails modules.
But this kind of codes will be a template supposed to run in rails.
Problem come when test encountered something like
my_module= Myprogram::Module.find_or_create_by
:module_code=>my_module.code,
:code=> scode,
:name=>sname
then
error: Undefined method find_or_create_by
My first though is to require (what) gem that has this method.
Can anyone help me out.?

Myprogram::Module is mongoid class using namespace to that class.
I fixed the error by.
require proper gem which is mongoid (thanks #marmeladze and mikwat)
require_relative to the class (without this will show NameError:
uninitialized constant Myprogram::Module::Mongoid
In my case here the code:
.
.
require 'mongoid'
require_relative '../lib/generators/myprogram/templates/app/models/myprogram/module.rb'

Related

RuntimeError: can't modify frozen Array (Rollbar, Rails 5.1 upgrade)

Unable to use rspec and rollbar after upgrading to rails 5.
Create a Rails 4 app
Upgrade gemfile to use rails 5
Try adding rollbar gem/support
Standard config/environment.rb:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
Error when running rspec:
An error occurred while loading {path to specific spec file}
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
RuntimeError:
can't modify frozen Array
# ./config/environment.rb:6:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
...
No examples found.
In most cases, that error is a red herring for something else.
When encountering it, don't get overwhelmed with the recurrent can't modify frozen Array error messages, and instead check the very first error that appears when running a spec.
For example:
Failure/Error: validate :uniqueness, if: 'should_be_unique?'
ArgumentError: Passing string to be evaluated in :if and :unless
conditional options is not supported. Pass a symbol for an instance
method, or a lambda, proc or block, instead.
Just to add one tip on top of Maxximo Mussini's answer.
If anyone can't find the first error on the terminal, please try to run RSpec on one file, i.e. rspec spec/models/user_spec.rb
You should be able to find the root case.
In my case, I haven't updated my local .env variables that is required by User model
Hope it helps
Debugging this is not easy but one possible solution is simple. It could be a naming conflict with Rollbar, possibly something getting monkey-patched. If you're seeing this RuntimeError but not using Rollbar, see other answer.
Add a Module ("namespace" of your choice) around your app class definition in config/application.rb.
The module won't affect much. The only difference I could find is when printing out your app it will now appear as (that's how we found the fix vs a new working app):
<MyTestAPP::Application ...> instead of <Application ...>
Change:
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
To:
Module MyTestApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
end

Rails public_activity gem rspec setting error

I am integrating public_activity (version 1.5) gem to my Rails 4 app.
Trying to set up tests as specified in their wiki, I write the following:
#spec_helper.rb
require 'public_activity/testing'
PublicActivity.enabled = false
However, trying to run my specs I get the following error:
/my_app/spec/spec_helper.rb:24:in <top (required)>': undefined methodenabled=' for PublicActivity:Module (NoMethodError)
Looking at Public Activity module source code I can clearly see enable= method there.
Can you please advise me what am I doing wrong here?
Looking at the source, testing.rb does not require PublicActivity where enabled= is defined, so I believe you'll need to do
require 'public_activity'
require 'public_activity/testing'
like it's done in their test_helper.rb.
The documentation seems to be incorrect.
I was able to get it to work like this:
PublicActivity::Config.instance.enabled = false
Update: Jan Klimo's answer is the correct way.

RSpec doesn't load my class even though it loads in the Rails console

I've got a class inside app/models/parser called data.rb with contents :
class Parser::Data
def method1
end
end
Nothing fancy at this point. I'm trying to write a test for it before implementing too much, just did default RSpec install for Rails.
My RSpec file is in spec/models/parser/data_spec.rb and is very basic so far:
require 'spec_helper.rb'
describe Parser::Data do
let(:parser) { Parser::Data.new }
end
When I run the test I get this error:
spec/models/parser/data_spec.rb:3:in `<top (required)>': uninitialized constant Parser (NameError)
I tried placing module Parser around the Data class in the same directory app/models/parser, also I've tried moving it to lib/parser doing the same module wrapping class, and added lib/parser to autoload in the application.rb but nothing has worked so far.
What am I doing wrong?
require 'rails_helper' instead of spec_helper. Requiring only spec_helper reproduces the issue for me, and requiring rails_helper fixes it. More on spec_helper vs. rails_helper (including performance implications) here: How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?
I reproduced the problem by running RSpec with bundle exec rspec. If I run RSpec with bin/rspec (that's a binstub generated by the spring-commands-rspec gem) it doesn't care which helper file I require. I guess spring loads more eagerly.

How to resolve Rails, GeoRuby (and postgis/postgresql): NameError: uninitialized constant Point?

when i am running the code on rails console as:
Point.from_x_y(88.365805,22.543538)
Then getting error as:
NameError: uninitialized constant Point
Can anyone help me to remove this error?
I am running require 'gems' before running this code as:
require 'geo_ruby'
require 'geo_ruby/ewk'
It's namespaced, so if you're using it rails instead of pure ruby, you need to call the full namespace of the class. Your query will work if you call instead:
GeoRuby::SimpleFeatures::Point.from_x_y(88.365805,22.543538)

How to access a namespace method in Ruby console?

Found this post Include namespace in Rails 3.1 console but it doesn't seem to work.
The following lib/task defined and it works from the command line: rake namespace_name:task_name.
How to call a method method_name in namespace_name from within the console, without calling the task?
rails console
namespace_name::task_name
NameError: undefined local variable or method 'namespace_name' for main:Object
irb namespace_name
NameError: undefined local variable or method 'namespace_name' for main:Object
Working in Rails 3.07, Ubuntu.
If you want to call a method defined inside a .rake file you do something similar to what #Nate said, but instead of calling the raketask, call the method:
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
method_name(arg1, arg2)
It feels kind of strange that you don't need to specify the namespaces but I just tried this and it worked.
You're confusing two different kinds of "namespaces" - Ruby modules can perform the task of "namespacing" Ruby code; Rake namespaces are only used within Rake to categorize tasks, and they don't create a module namespace.
The page you linked only works with Ruby module namespaces.
If you want to call Rake tasks from the Rails console, it's a bit more involved...
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
Rake::Task['namespace_name:task_name'].invoke
Or just call it on the command line from within the Rails console -
%x[rake namespace_name:task_name]

Resources