Rails 3.2. Create tests for existing model. Scaffold error - ruby-on-rails-3

I would like to generate tests (Test::Unit) for existing model using rails-3.2 scaffolding.
I've tried several commands and have got the following mistake:
invoke active_record
The name 'MyModel' is either already used in your application or reserved by
Ruby on Rails. Please choose an alternative and run this generator again.
I have used the following commands and result was the same:
rails g scaffold MyModel --skip
rails g scaffold MyModel -s
rails g scaffold MyModel -t -s
I also have tried to use gem schema_to_scaffold to point all the MyModel fields for scaffold but it gives no result.
rails generate scaffold MyModel fio:string #..all fields of my model..# -s
Thanks in advance!

Related

Ruby On Rails Tutorial --> Encoding::InvalidByteSequenceError in Users#index

Folks, I am following Ruby on Rails Tutorial and working on the DemoApp in Chapter 2. My env is :
Win 7
Ruby 1.9.3
Rails 4.0.2
Just did the following:
c:\rails generate scaffold User name:string email:string
I brought the server up using :
c:\rails s
Now when I go try to view the users page on a web browser using the URI:
http://"localhost":3000/users ---> doing "localhost" instead of localhost
I get the error mentioned in the title (Encoding::InvalidByteSequenceError in Users#index).
I am new to rails.
Please help?
thanks

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

acts_as_commentable_with_threading uninitialized constant Post::Comment Rails 3.1.3

I have upgraded my application from Rails 2.3.5 to Rails 3.1.3. I had acts_as_commentable_with_threading and awesome_nested_set as plugins. Now I have added in GemFile for Rails 3.1.3 as below
gem 'awesome_nested_set'
gem 'acts_as_commentable_with_threading'
I have code in the model as below
class Post < ActiveRecord::Base
acts_as_commentable
end
The migration for acts_as_commentable_with_threading is already applied in the database.
I have statement in the view as
<%= pluralize(post.root_comments.size, "comment") %> on this post
When I try to load that view I get error at that line as
uninitialized constant Post::Comment
What can be the problem. Please help me.
Thanks in advance!
You might have forgot to run the generator and the migration afterwards:
rails generate acts_as_commentable_with_threading_migration
or
rails generate acts_as_commentable_upgrade_migration
and after:
bin/rake db:migrate
It's first steps of github repo instructions

Create a rails 3 plugin with RSpec

The question explains itself...
I wanna test a new rails plugin with rspec, are there any specific generators, gems?
I used rails g plugin blabla and I got an error on rspec generator: error rspec [not found].
FYI - there is an open github issue about this: https://github.com/rspec/rspec-rails/issues/#issue/258.

annotate-models: Nothing annotated!

My situation:
I have added the gem annotate-models (1.0.4) to my rails project.
Ran "bundle install". The gem is listed when I do "bundle list".
I generated a simple model using "rails generate model ..."
I executed: "rake db:migrate"
I executed: "annotate"
The result:
6. "annotate" returned "Nothing annotated!".
7. My model class remains without annotations.
Any idea as to what might be going wrong?
Thank you!
Apologies. I don't why this is, but I created the model Object in question in the rails console and saved an example to the db. Then, I re-ran "annotate" and the model was successfully annotated.
Try this. A few commands dont run right without the exec
bundle exec annotate