rake aborted! uninitialized constant Object::Country, why can't see model? - ruby-on-rails-3.1

I have rails 3.1, I am trying to populate data with seeds.rb
I have a model Country which is migrated into a countries table
But it seems that rails can't see Country model from seeds.rb
I get this error:
rake aborted!
uninitialized constant Object::Country
Tasks: TOP => db:seed
my seeds.rb file looks like this:
# encoding: UTF-8
Country.delete_all
my Country model:
class Country < ActiveRecord::Base
has_many :students
has_many :instructors
end
any idea please ?
EDIT
I am in development environment, with config.threadsafe!

Related

Rails gem - Public Activity for earlier posts

The gem does not recognize earlier post which were created before the gem was added.
It only started working, when fresh posts were created. Why was that?
And, how to have those earlier posts get covered by public_activity
Thanks.
Gem setup according to author site.
You've to create the old activities manually using create_activity method. I created a rake task for this.
task public_activity_migration: :environment do
User.find_each do |user|
[:comments, :friends].each do |relation|
user.send(relation).find_each do |obj|
obj.create_activity :create, owner: user, created_at: obj.created_at
print "."
end
end
end
end
The code above will create activities for the comment and friend model. If you're not using strong params you also need to allow the created_at attribute to be set on the PublicActivity::Activity model. This can be done by adding the following code before running your task.
class PublicActivity::Activity
attr_accessible :created_at
end

How to include id in slug using friendly_id

I am using friendly_id gem to have pretty urls in my application.I want an url like:
localhost:3000/posts/1/my-first-post
I want to do it using the friendly_id gem as i want to use the History module to avoid 404 error.I have tried but can't include the id in the slug.The code i am using is follows:
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :pretty_url, use: [:slugged, :history]
def pretty_url
"#{id}/#{title}"
end
end
But it is not including the id in the slug.Rather it is using the slug as if only title is used.
I think I remember reading somewhere that FriendlyId runs into issues with slashes within your slug function. Try "#{id}-#{title}" instead to see if that's the case.
The id of Post is set after the entry is saved. pretty_url doesn't work because the id doesn't exist yet.
You can get this behaviour without using the friendly_id gem by doing:
def to_param
"#{id} #{title}".parameterize
end

What folders does Rails 4.1 include in autoload

I am writing a Ruby on Rails 4.1 app with Ruby 2.11. The structure is Domain -> Team -> User. So, a user belongs to a team and I sometimes need to make a team from a domain (so they are associated). Think Starbucks -> NY Central Team -> Mr Barista.
I have made some builder classes and put them in app/builders, but when I try to use the class it says uninitialized constant in the Rails console. So, for example, I have the file in app/builders/team_builder.rb:
class TeamBuilder
attr_reader :domain, :params
def initialize(domain, params = {})
#domain = domain
#params = params
end
def build
domain.teams.build(params)
end
end
But when I type TeamBuilder.new(domain, name: 'Team name here!') I get the response
NameError: uninitialized constant TeamBuilder
It seems like it does not recognise the new class I have added above, which makes me think it is not loading it. But I thought that all sub-directories in app/ were loaded.
Totally stumped on this one, and I cannot find a guide or documentation on this (maybe it's there - somewhere...)
Go into your rails console (rails c) and type the following:
YourAppName::Application.config.eager_load_paths
(where YourAppName is whatever it is called in your environment.rb where initialize! is called). This should show you all the paths automatically loaded into your application (before customizing).
It looks like the automatic path adding magic is happening in here https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb
The docs indicate you can run:
bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'
to view in terminal

ruby on rails: How to create table for a new model

I use
rails generate model mynewmodel string:name string:description
to generate a new model. How do I deploy this new model to my develop databse ? I already have a bunch of databases in my sqlite db.
I have tried
rake db:migrate
it seemed having trouble to generate this new table in db.
update: added error message
== CreateMynewmodels: migrating ===============================================
-- create_table(:mynewmodels)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `name' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x3ad5c50>
Tasks: TOP => db:migrate
Thanks
The order of your fieldname:type combo is incorrect. Try
rails generate model mynewmodel name:string description:string
The error in rails generate model mynewmodel string:name string:description
You should swap string and name
rails generate model mynewmodel name:string description:string
Use name:string instead of string:name same for description
Great article for advanced usage:
Advanced Rails model generators
Pay attention that you have to wrap parameter price:decimal{10,2} to
quotes. It's vital and you may have incorrect behavior of generator if
you don't do it.

carrierwave - rails 3.1- undefined method: image_will_change

I get an error that look like this:
undefined method `post_image_will_change!' for #<Post:0xf4e9184>
app/controllers/posts_controller.rb:43:in `new'
app/controllers/posts_controller.rb:43:in `create'
I've included this in my "post" model:
attr_accessible :title, :name, :content, :post_image
mount_uploader :post_image, PostImageUploader
and in _form.html.erb I've added:
:html => { :multipart => true }
I looked CarrierWave Error but that doesn't help me.
Any clues of what generates that error? I've migrated the database and so forth (followed the railscasts guide on carrierwave exactly..)
The OP comments that he fixed it, however there's no answer set so I thought I'd add one for people coming across this in the future, which included myself until I figured it out :)
undefined method `x_will_change!' for # happens if you forget to add a column in your model's db table. If you have a model User and a AvatarUploader, with the uploader mounted as in the Carrierwave docs:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
Then the error will read
undefined method `avatar_will_change!' for #<User:0x00...>
To fix it (based on this example) add a column in a migration run the following in the console:
rails g migration AddAvatarToUsers avatar:string
This will generate the following migration:
class AddAvatarToUsers < ActiveRecord::Migration
def change
add_column :users, :avatar, :string
end
end
Then migrate to apply the change (again in the console):
rake db:migrate
I suppose that author just forgot to run:
rake db:migrate
ALso, if you met such error inside of your tests then you should run:
rake db:test:prepare
Also, for anyone getting this error on heroku, you need to run
heroku run rake db:migrate
and
heroku restart
in the terminal after adding/removing fields/tables from your database.
Kreek, this is obviously a minor oversight, as most people would have realized by now, you probably meant to run this command, as one should, outside the console, otherwise, one would get the following:
'NameError: undefined local variable or method `migrate' for main:Object'.
I had similar problem but mine was because I was copying and pasting codes and forgot to delete
mount_uploader :picture, PictureUploader
from my model which did not use pictures.
Hope this help others in future who could not figure out what happened

Resources