undefined method `has_attached_file error paperclip - ruby

I am trying to use paperclip without rails(but trying to connect db created by rails).
Using bundler to require gems.
here are my models
class RailsDB < ActiveRecord::Base
establish_connection $db_config[:rails_db]
end
class VoiceCall < RailsDB
belongs_to :campaign
set_table_name :voice_calls
has_attached_file :sound_file
validates_attachment_presence :sound_file
end
If I try to run the program it throws the error
undefined method `has_attached_file
any idea y?
Edit:
my gem file
source "http://rubygems.org"
gem 'activerecord', '< 3.1', :require => 'active_record'
gem 'mysql2', '< 0.3'
gem "paperclip", "~> 2.4"
I require gems using
require "bundler/setup"
Bundler.require(:default)
One more observation. I started irb and required active record and then paperclip. and ran this
p ActiveRecord::Base.methods.select{|m| m =~ /has_attached_file/}
It returns empty list.
but when I open rails console (using "rails c") the statement works and returns the value.( Both are using same gems)

This error means the Paperclip gem is not loaded correctly (or at all) within your application.
Can you post your Gemfile and config/preinitializer.rb?
Is bundler working to successfully load other Gems in your environment?
I've seen this happen when Bundler was not configured correctly for a Rails project causing gems to fail to load. The paperclip model references are the canary in the coal-mine for this larger problem.

had the same issue.
using
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
instead helped out.

Related

Error while running a Sinatra Application

I'm trying to run a Sinatra application with the most basic app.rb:
require 'sinatra/activerecord/rake'
require 'bundler/setup'
Bundler.require(:default)
require_relative './config'
require_relative './models/star'
require_relative './models/planet'
require_relative './models/moon'
require_relative './models/astronaut'
get '/' do
erb :index
end
After using Bundle and creating Gemfile.lock I'm keep getting this error:
You have already activated activesupport 4.0.2, but your Gemfile requires activesupport 3.2.16. Using bundle exec may solve this. (Gem::LoadError)
My Rakefile is:
source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-reloader'
gem 'sinatra-activerecord'
gem 'activerecord', '~> 3.2.13'
gem 'rake'
gem 'pg'
gem 'pry'
I'll be grateful for any suggestions.
You have both ActiveRecord 4.0.2 and 3.2.13 installed on your system. The first line of your app requires sinatra/activerecord/rake which in turn requires activerecord, without specifying which version. This activates and loads 4.0.2 – the latest version.
In the next line you try to set up Bundler. Bundler now tries to activate version 3.2.13 of ActiveRecord as specified in your Gemfile, but can’t since a version is already activated, so you get an error.
To fix, simply make sure you call require 'bundler/setup' first, before you require any other files. This will ensure any files you require will be compatible with your Gemfile.
Alternatively you could remove the call to require bundler/setup and make sure you always start your app using bundle exec.

requiring old versions of gems

Since none of the tools I have handy on my Windows laptop can export decent CSV files from SQL Server to save their lives, I figured I'd roll my own in a few lines of Ruby.
Because I'm hitting the version of SQL Server from the turn of the century, I have to use an old version of activerecord-sqlserver-adapter. As far as I can tell I'm doing this correctly, and should be using activerecord ~> 2.2.3 and the 2-3-stable branch of activerecord-sqlserver-adapter, but I am getting an error that complains it is Unable to activate activerecord-sqlserver-adapter-3.2.12, because activerecord-2.2.3 conflicts with activerecord (~> 3.2.0). Here's my code (without anything specifically related to CSVs):
#!/usr/bin/env ruby
gem 'activerecord', "~> 2.2.3"
gem 'activerecord-sqlserver-adapter', github: 'arthrex/activerecord-sqlserver-adapter', :branch => '2-3-stable'
require 'activerecord'
require 'activerecord-sqlserver-adapter'
require 'pry'
ActiveRecord::Base.establish_connection(
:adapter => "sqlserver",
:mode => "odbc",
:username => "c3",
:password => "92641",
:dsn => "Connect3"
)
ActiveRecord::Base.table_name_prefix = 'dbo.'
class Dwnld_Hdr < ActiveRecord::Base
end
pry
Why is it trying to load activerecord (~> 3.2.0) in the first place?
I think in this case you might probably use bundler, because then it will allow you to run the specific gem sets.
Read here and here to get an idea on how to use bundler in a non-Rails project.

How do I use mock_models with RSpec outside of Rails?

I am currently working on a Ruby project that comprises of a Redis DB, EventMachine and Sinatra (for a lightweight API). We're using RSpec for our testing and up until this point, everything has been going great.
Now, however, we need to start talking (minimally) to a MySQL database. We've decided to go the route of ActiveRecord (just because it's SO nice!) and everything is going smooth so far. However, we are running into a small problem with our tests. I found some resources online using mock_model and mock with RSpec but, I keep getting:
undefined method `mock_model'
undefined method `mock'
errors. What gem to I need to use for these to work without installing the entire Rails framework (just seems like overkill).
OR
What other mocking/stubing methods/gems should I be using to mock my AR models? I really enjoy the flexibility of a syntax like:
#user = double(NS::UserAccount)
#car = double(NS::Car)
NS::UserAccount.stub(:find).with(#valid_user_id).return(#user)
NS::Car.stub(:find).with(#valid_car_id).return(#car)
UPDATE
My Gemfile looks like the following:
group :test do
gem 'rspec', '~> 2.8.0'
gem 'rspec-expectations', '~> 2.8.0'
gem 'rspec-mocks', '~> 2.8.0'
gem 'autotest-standalone'
gem 'em-spec', '~> 0.2.6'
gem 'rack-test', '~> 0.6.1'
gem 'factory_girl', '~> 3.1.0'
end
Also, at the top of the spec_helper.rb file, I have the following:
require 'bundler/setup'
Bundler.require(:test, :development)
For the Active Record models RSpec has method mock_model, which automatically creates several stubs, common for all Ruby on Rails models, and accepts hash.
So to use mock_model outside of Rails you should have connected Active Record and rspec-rails extension.
To make sure you can look at here where's mock_model is defined.

Error loading Active Record gem with sinatra app using RVM

I set up a project level RVM gemset for a sinatra app I am starting that will connect to a local database with Active Record. In order to test it I tried to run the below test app:
test.rb
require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'activerecord'
class Article < ActiveRecord::Base
end
get '/' do
Test.establish_connection(
:adapter => "sqlite3",
:database => "hw.db"
)
Test.first.content
end
(Taken from the answer to this question: What's the best way to talk to a database while using Sinatra?)
When I run ruby -rubygems test.rb I get this error:
/Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- activerecord (LoadError)
I've already installed the Active Record gem and it shows up in gem list and rvm current displays the correct gemset. I am new to RVM and I think this is something to do with the it not having the correct load path but I feel like I've set everything up correctly so I'd appreciate suggestions on what's wrong. Thanks.
As far as I can tell require 'activerecord' has been deprecated. Try using
require 'active_record'
instead.
If you have not already installed the activerecord gem, you will also get that error:
Open a command prompt and run these commands in the terminal:
#Find if the active record gem is already installed on your computer:
gem query --local
#See the downloadable gems, and see if activerecord is still available:
gem query --remote --name-matches activerecord
#Install your gem:
gem install --remote activerecord
#See if it installed successfully and is in the installed gem list:
gem query --local
Here is some code that uses the ActiveRecord gem to see if everything is configured right:
#Ruby code
require 'active_record'
class Dog < ActiveRecord::Base
has_many :dog_tags
end
puts "activerecord gem is installed";
If everything is working, it will print "activerecord gem is installed" without any errors.

Rails 3 Authlogic - 'acts_as_authentic' undefined

I'm getting the following error:
NameError (undefined local variable or method `acts_as_authentic' for #<Class:0x1037e6310>):
app/models/user.rb:2
app/controllers/user_controller.rb:3:in `new'
I'm using Rails 3.0.0, with Ruby 1.8.7. Authlogic is in my Gemfile as follows:
gem 'authlogic', :git => "git://github.com/binarylogic/authlogic.git"
The entire contents of my User.rb file are as follows:
class User < ActiveRecord::Base
acts_as_authentic
end
I get the same error whether through 'rails console' or through the server. I have restarted the server more times than I can count. Please help, I can't figure it out.
Use a version of authlogic patched for Rails 3
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
Or even better. Use Devise
Is the Authlogic Gem installed ?.
Please do
Bundle install
this should solve your problem.
gem "authlogic", "2.1.6"
Then
Bundle install
Hope it'll helpfull for you. :)
create a file in config/initializers as restful_authentication.rb
and paste this inside the file and restart server and try
require 'authenticated_system'
require 'restful_authentication/authentication'
require 'restful_authentication/authentication/by_password'
require 'restful_authentication/authentication/by_cookie_token'
require 'restful_authentication/authorization/aasm_roles'
require 'restful_authentication/authorization/stateful_roles'
require 'restful_authentication/trustification/email_validation'

Resources