friendly_id 4.0.8 with rails 3.0.1 rails generate friendly_id not working? - ruby-on-rails-3.1

I am using friendly_id 4.0.8 with rails 3.0.1. I want to use the history feature.
When I type the command rails generate friendly_id, it fails with the following error message:
[WARNING] Could not load generator "generators/friendly_id_generator". Error: ActiveRecord is not missing constant Migration!.
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:479:in `load_missing_constant'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:183:in `rake_original_const_missing'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in `each'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:181:in `rake_original_const_missing'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/usr/lib/ruby/gems/1.8/gems/friendly_id-4.0.8/lib/generators/friendly_id_generator.rb:8
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:225:in `load_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:591:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:225:in `load_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.
Is there some different syntax to make it work ?

I have got it working currently by manually generating a migration script with
rails generate migration create_friendly_id_slugs
Then replace the contents of the migration script with that from gems/friendly_id-4.0.8/lib/friendly_id/migration.rb as follows:
class CreateFriendlyIdSlugs < ActiveRecord::Migration
def self.up
create_table :friendly_id_slugs do |t|
t.string :slug, :null => false
t.integer :sluggable_id, :null => false
t.string :sluggable_type, :limit => 40
t.datetime :created_at
end
add_index :friendly_id_slugs, :sluggable_id
add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true
add_index :friendly_id_slugs, :sluggable_type
end
def self.down
drop_table :friendly_id_slugs
end
end
Then run rake db:migrate

Related

Creating an ActiveRecord database for a webcrawler. Having issues adding index

I'm working on a three part program for a webcrawler program for school. My first step is to just create the database using activerecord and knowing that it will be queried later using sqlite3. The database actually does get created with my current program. However, it errors out when gets to "add_index". I'm not great with databases in general so guidance is appreciated.
Before I put in my code the error is:
crawler_create.rb:23: syntax error, unexpected ',', expecting ')'
add_index (:pages,[:url], unique=> true)
Here's what I've come up with so far:
require 'active_record'
require 'sqlite3'
db=SQLite3::Database.new("crawler.db")
ActiveRecord::Base.establish_connection(adapter:'sqlite3',database:'crawler.db')
ActiveRecord::Schema.define do
create_table :pages do |t|
t.string :url, :null => false
t.string :title
t.string :content_type
t.date :last_modified
t.string :status
end
end
add_index (:pages,[:url], unique=> true)
ActiveRecord::Schema.define do
create_table :links do |t|
t.integer :from_page_id, :null => false
t.integer :to_page_id, :null => false
end
end
add_index(:links,[:from_page_id,:pages_id], :unique =>true)
ActiveRecord::Schema.define do
add_foreign_key :from_page_id, :pages_id
add_foreign_key :to_page_id,:pages_id
end
It looks like you got a simple syntax error here:
add_index (:pages, [:url], unique: true)
I'd suggest that you use a consistent hash syntax, either using hash rockets => or the newer syntax unique: true.

Error message when running Rake command in Rails app

I'm trying to create posts in my Rails app by pulling data from a CSV file.
When I try to run a Rake command, I get the error message below. What's wrong with this code?
SEED.RAKE FILE
require 'csv'
namespace :csv do
desc "Import CSV Data for Michelin Star Restaurants"
task :post => :environment do
csv_file_path = 'db/data.csv'
CSV.foreach(csv_file_path) do |row|
Post.create({
:name => row[1],
:address => row[2],
:city => row[3],
:michelin_status => row[4],
:website => row[5],
:phone => row[6],
:longitude => row[7],
:latitude => row[8],
:inthenews => row[9],
:google_reviews => row[10],
})
end
end
end
ERROR MESSAGE FROM CONSOLE
Faisals-Air:dinner fkhalid2008$ rake csv:post
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/Users/fkhalid2008/dinner/lib/tasks/seed.rake:11:in `block (2 levels) in <top (required)>'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => csv:post
(See full trace by running task with --trace)
DB MIGRATE FILE
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :name
t.string :inthenews
t.string :michelin_status
t.string :google_reviews
t.string :address
t.string :city
t.string :website
t.integer :phone
t.integer :longitude
t.integer :latitude
t.timestamps null: false
end
end
end
CSV FILE SCREENSHOT
This is because somewhere in your file there are some invalid bytes.
To avoid this issue, you can use scrub method like this:
CSV.foreach(csv_file_path) do |row|
Post.create({
:name => row[1].scrub,
:address => row[2].scrub,
:city => row[3].scrub,
:michelin_status => row[4].scrub,
:website => row[5].scrub,
:phone => row[6].scrub,
:longitude => row[7].scrub,
:latitude => row[8].scrub,
:inthenews => row[9].scrub,
:google_reviews => row[10].scrub,
})
end
Update
Try to specify the encoding type when you read the CSV file like this:
CSV.foreach(csv_file_path, "r:ISO-8859-1") do |row|
. . .
. . .
end
You can simply convert the original csv file to utf8 with nkf.
$ nkf -w --overwrite db/data.csv

`rake db:migrate` fails with a NoMethodError

So everything has worked on my project to this point which is 6.0 Adding a second model. I did
rails generate model Comment commenter:string body:text article:references
And this generated the correct files which I opened and looked at.
app/models/comment.rb
And the migrate file
_create_comments.rb (I left off the date stamp)
When I run rake db:migrate I get the following errors:
== 20150709191058 CreateComments: migrating ===================================
-- create_table(:comments)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
private method `test' called for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007fe7984adf58>/Users/johnlarkin/Sites/blog/db/migrate/20150709191058_create_comments.rb:5:in `block in change'
/Users/johnlarkin/Sites/blog/db/migrate/20150709191058_create_comments.rb:3:in `change'
NoMethodError: private method `test' called for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007fe7984adf58>
/Users/johnlarkin/Sites/blog/db/migrate/20150709191058_create_comments.rb:5:in `block in change'
/Users/johnlarkin/Sites/blog/db/migrate/20150709191058_create_comments.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Has anybody run into a similar error when working on this project I've just started learning ruby and rails.
Thanks for your help.
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.test :body
t.references :article, index: true
t.timestamps null: false
end
add_foreign_key :comments, :articles
end
end
I think you typed "test" instead of "text" in the rails generate command. You could remove all the new files and start over, or change "t.test" to "t.text".

MySQL and rails syntax error

I configured rails to work fine with Rails 3. I am trying to create a migration, and here it is its code:
class CreateObservations < ActiveRecord::Migration
def change
create_table :observations do |t|
t.integer :user_id
t.integer :start
t.integer :end
t.string :videoID
t.string :event
t.string :content
t.timestamps
end
add_index :observations, [:user_id, :created_at]
end
end
now when I run 'rake db:migrate' I get this strange error:
why?
demo_app/test/factories/observations.rb:7:syntax error, unexpected tINTEGER, expecting keyword_end
demo_app/test/factories/observations.rb:12: syntax error, unexpected keyword_end, expecting $end
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I am NOT doing any testing now. Just development. so I run this:
rake db:migrate RAILS_ENV=development
and I get the same error.
here is the code in the factory girl which I dont want to include!!!
FactoryGirl.define do
factory :observation do
user_id 1
start 1
end 1
videoID "MyString"
event "MyString"
content "MyString"
end
end
It's probably because of using end field try to change it to something different

Why am I getting the error "undefined method `name' for" in a Formtastic / haml view where "name" is a property on the model?

This is probably something stupid, but I don't know nearly enough about rails & ruby to see it. I have the following schema & view but I am getting the error mentioned below. Business inherits from a Devise Account so thats where the email & password come from.
Any help would be greatly appreciated, thanks!
schema:
create_table "businesses", :force => true do |t|
t.string "name"
t.string "street"
t.string "city"
t.string "zip"
t.datetime "created_at"
t.datetime "updated_at"
end
View:
#registrationForm
-semantic_form_for(resource, :as => resource_name, :url=> registration_path(resource_name)) do |f|
=f.input :name
=f.input :email
=f.input :password
=f.input :password_confirmation
=f.buttons
Error:
undefined method 'name' for
<Business:0x000000052690f8 > Extracted source (around line #3):
Edit
Controller
class BusinessesController < Devise::RegistrationsController
respond_to :html
def new
super
#business = Business.new
end
end
Routes.rb
devise_for :accounts
devise_for :businesses, :controllers => { :registrations => "businesses" }
Model
class Business < Account
end
console after reloading schema
k = Business.new ( :name =>"test" )
^
(irb):1: syntax error, unexpected ')', expecting $end
from /home/chance/.rvm/gems/ruby-1.9.2-p180#global/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180#global/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180#global/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
You have a table named 'accounts' and a table named 'businesses'.
Account is being made by devise, and has all its attributes, and points to the 'accounts' table.
Business inherits from Account, and therefore is using Rails' STI (single table inheritance) features. It therefore points to the 'accounts' table as well.
If you were to have Business < ActiveRecord::Base it would point to your 'businesses' table. ActiveRecord's STI mechanism is very strange.
I think you need to think more about how you want your data model to work. Perhaps Business should belong_to :account and have an according :account_id.
Either that or you could add all the 'businesses' columns to the accounts table.
Try to load your schema again
rake db:schema:load

Resources