Rails 4.2: file_field, string contains null byte error - ruby-on-rails

After upgrading to Rails 4.2 my code to save an uploaded file to the database (pg) fails with the error message: 'string contains null byte'.
I've created a new Rails 4.2 app with just one model and a binary field.
create_table "entries", force: :cascade do |t|
t.string "name"
t.binary "file"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Using the console:
:001 > entry = Entry.new
=> #<Entry id: nil, name: nil, file: nil, created_at: nil, updated_at: nil>
:002 > f = File.open('public/icon-sidesearch.png', 'rb') { |io| io.read }
=> "\x89PNG\r\n\u001A\n\u0000\... more binary data"
:003 > entry.file = f
ArgumentError: string contains null byte
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `unescape_bytea'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `type_cast_from_database'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/type/binary.rb:26:in `changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute.rb:54:in `changed_in_place_from?'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:74:in `attribute_changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:149:in `block in changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `select'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:141:in `attributes_changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:62:in `changed_attributes'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activemodel-4.2.0/lib/active_model/dirty.rb:173:in `attribute_changed?'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:107:in `save_changed_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:50:in `__temp__6696c656'
from (irb):5
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
... 9 levels...
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
from /Users/jose/.rvm/gems/ruby-2.1.2#rails42_sice/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Any ideas what might be happening?

As mentioned here, that is a Rails bug and fixed on 4.2.1
https://github.com/rails/rails/pull/17680

Related

Error in rails console when interacting with database tables

I've been getting this persistent error every time I try to update my table in rails.
For example if my code in the schema.rb file is:
create_table "books", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
If I go into the rails console and write the command:
book = Book.new(title: "a book", description: "a book description")
it gives me the following error:
Errno::EBADF (Bad file descriptor)
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:753:in `output_value'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:545:in `block (2 levels) in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:704:in `signal_status'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:538:in `block in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `loop'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `block in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:537:in `eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:472:in `block in run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:400:in `start'
Maybe IRB bug!
I can continue and write book.save which will update the new book into the database however I not sure if this error message is a concern. It wasn't there previously when running this command and is now there on all projects when trying to update any tables.
I've completely reinstalled Ruby 2.7 and RubyMine and this error still returns. I'm not sure what this problem is caused by.
Thank you for any advice.
(Info: working on Windows machine on RubyMine. Using rails 5.2.4 and Ruby 2.7.1)
That appears to be a known problem with IRB and Ruby 2.7 so could you please add your vote there:
https://youtrack.jetbrains.com/issue/RUBY-26249

Uninitialized constant Types::QuotationReturnType

I am trying to create a query to return a filtered list of quotations from a "quotations" table.
The code is inside an engine name "cpq"
types/quotation_type.rb
module Cpq
module Types
class QuotationType < ::Cpq::Types::BaseObject
field :id, ID, null: false
field :inc_id, Integer, null: false
field :name, String, null: false
field :description, String, null: true
field :buyer_id, ID, null: true
field :seller_address, String, null: true
field :buyer_address, String, null: true
field :quotation_no, String, null: false
field :updated_at, ::Types::DateTimeType, null: false
end
class QuotationReturnType < ::Cpq::Types::BaseObject
field :success, Boolean, null: false
field :quotation_data, [::Cpq::Types::QuotationType], null: true
field :page, Integer, null: true
field :total, Integer, null: true
field :total_count, Integer, null: true
end
end
end
types/query_type.rb
Cpq::Types::QueryType = GraphQL::ObjectType.define do
name "Query"
field :quotations do
type ::Cpq::Types::QuotationReturnType
argument :id, types.ID
argument :name, types.String
argument :buyer_ids, types[types.ID]
argument :statuses, types[types.String]
argument :page, types.String
argument :page_limit, types.String
argument :sort_by, types.String
...
...
resolve -> (obj, args, ctx) {
quotation_ids = []
quotation_ids << args[:id] if args[:id].present?
name = args[:name]
...
...
return {
success: true,
page: page,
total: total,
total_count: total_count,
data: quotation.as_json(::Cpq::Quotation.as_json_query)
}
}
end
end
However, on hitting the query "quotations", i get an error
Traceback (most recent call last):
10: from -e:1:in `<main>'
9: from /Users/sourabhmodi/.rvm/rubies/ruby-2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
8: from /Users/sourabhmodi/.rvm/rubies/ruby-2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
7: from /Users/sourabhmodi/Documents/workspace/rails/Cogoport-api/bin/rails:9:in `<top (required)>'
6: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
5: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
4: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
3: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
2: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/console.rb:65:in `start'
1: from (irb):14
NameError (uninitialized constant Cpq::Types::QuotationReturnType)
On Rails Console
2.5.0 :023 > ::Cpq::Types::QuotationReturnType
Traceback (most recent call last):
10: from -e:1:in `<main>'
9: from /Users/sourabhmodi/.rvm/rubies/ruby-2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
8: from /Users/sourabhmodi/.rvm/rubies/ruby-2.5.0/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
7: from /Users/sourabhmodi/Documents/workspace/rails/Cogoport-api/bin/rails:9:in `<top (required)>'
6: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
5: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
4: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
3: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
2: from /Users/sourabhmodi/.rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/commands/console.rb:65:in `start'
1: from (irb):23
NameError (uninitialized constant Cpq::Types::QuotationReturnType)
2.5.0 :024 > ::Cpq::Types::QuotationType
=> Cpq::Types::QuotationType
2.5.0 :025 > ::Cpq::Types::QuotationReturnType
=> Cpq::Types::QuotationReturnType
When I try it on console, I realised that once I access the type Cpq::Types::QuotationType before, then it is able to access the class QuotationReturnType. But its gives me an error if I simply try accessing Cpq::Types::QuotationReturnType. Probably unable to load it or something.
Confused. Please help.
P.S. I am new to graphql, so please ignore any naive/redundant code, if mentioned.

Confusion over assocation and getting info from console

Confusion over trying to get information from Rails Console
I have the following models: Article & Comment.
For Article, it's a has_many :comments
For Comment, it's belongs_to :article
Let's assume they're associated, and functional. They are by the way.
Here's the schema:
ActiveRecord::Schema.define(version: 20160312052519) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["article_id"], name: "index_comments_on_article_id"
end
These are the routes:
resources :articles do
resources :comments
end
When I run commands like:
Article.first.comments
Comment.first.article
Comment.first.article.title
Comment.first.article.text
Comment.first.commenter
Comment.first.body
They all work.
But when I try to run the reverse on Articles like:
Article.first.comments.commenter
Article.first.comments.body
Article.all.comments
They don't work. Why not?
EDIT
This is what I get when I run Article.first.comments.commenter
Article.first.comments.commenter
Article Load (23.0ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT 1
NoMethodError: Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = ? [["article_id", 1]]
undefined method `commenter' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x007fa32b7e3d00>
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):127
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /Users/aa/dropbox/beginningRails/guideblogagain/bin/rails:9:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/commands/rails.rb:6:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/command_wrapper.rb:38:in `call'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:185:in `block in serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `fork'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:156:in `serve'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:131:in `block in run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `loop'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application.rb:125:in `run'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/spring-1.6.4/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/aa/.rbenv/versions/2.1.4/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
Article.first.comments is an ActiveRecord_Associations_CollectionProxy object, which is similar to an array. If you want to get commenter for each comment you can use map
Article.first.comments.map(&:commenter)
Or you can get first comment's commenter:
Article.first.comments.first.commenter
Same thing with:
Article.first.comments.body # Won't work
Article.first.comments.map(&:body) # Returns an array (each comment's body)
Article.all.comments # Won't work
Article.all.map(&:comments) # Returns an array (each article's comments)
When you run your working example Article.first.comments, take a look at the actual return value. What do you see? The return is an array of objects, right? So say that Article.first has 10 Comments. When you run your query, ActiveRecord will return you an array of 10 Comment objects.
Now let's take a look at one of your non-working examples.
Say, Article.first.comments.commenter. Again we start with Article.first, which we know returns us the first Article object in our DB.
But the next step is to think about Article.first.comments.
We've already convinced ourselves that that's an array of Comment objects, right? And herein lies the problem -- commenter is an attribute on a single Comment object.
But you don't have a single Comment object -- you have an Array of Comment objects and commenter isn't defined on an Array.
The Article.first.comments.body issue is the same. Does that make sense?
One thing you could do, in your console, is the following:
Article.first.comments.each do |comment|
puts comment.commenter
end
And then you can get at all of your commenter/body values.
Using this line of thinking, I think you'll be able to convince yourself of the issue with Article.all.comments, but if not, feel free to comment and we can talk it out.
Here are the docs for Active Record relations, for reference. Cheers!

Rails mountable engine: insert data rows in migration

Inside Rails mountable engine I generated a model:
rails g model Sources domain:string source_alias:string is_social:boolean is_organic:boolean organic_query_param:string
Here is migration file (20131220160128_create_sourcebuster_sources.rb)
class CreateSourcebusterSources < ActiveRecord::Migration
def change
create_table :sourcebuster_sources do |t|
t.string :domain, null: false
t.string :source_alias
t.boolean :is_social, default: false
t.boolean :is_organic, default: false
t.string :organic_query_param
t.timestamps
end
add_index :sourcebuster_sources, :domain, unique: true
add_index :sourcebuster_sources, :is_social
add_index :sourcebuster_sources, :is_organic
Sourcebuster::Source.create domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p"
end
end
Copy it to dummy app:
rake sourcebuster:install:migrations
Migration file with the different name — 20131220164445_create_sourcebuster_sources.sourcebuster.rb — but content is the same as above.
Ok, bundle exec rake db:migrate
And it gives me an error, when trying to insert data row:
== CreateSourcebusterSources: migrating ======================================
-- create_table(:sourcebuster_sources)
-> 0.0364s
-- add_index(:sourcebuster_sources, :domain, {:unique=>true})
-> 0.0066s
-- add_index(:sourcebuster_sources, :is_social)
-> 0.0069s
-- add_index(:sourcebuster_sources, :is_organic)
-> 0.0046s
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant Sourcebuster::Source/Users/Alex/Ruby/sourcebuster/test/dummy/db/migrate/20131220164445_create_sourcebuster_sources.sourcebuster.rb:17:in `change'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Why insertion fails?
try inserting this method
class CreateSourcebusterSources < ActiveRecord::Migration
def migrate(direction)
super
# Create a default Sourcebuster
Sourcebuster::Source.create! domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p" if direction == :up
end
#def change
...
end

adding values to table - ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord

ok guys i have a problem - becouse i created a model and table AdminUser and then i trying to add values to it and then console print me ... any idea?
1.9.3-p286 :020 > AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :021 > AdminUser.all
AdminUser Load (0.2ms) SELECT "admin_user".* FROM "admin_user"
=> []
1.9.3-p286 :022 > me = AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :023 > me.new
ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:91:in `instance_method_already_implemented?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:263:in `block in define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
from <internal:prelude>:10:in `synchronize'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:168:in `respond_to?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:398:in `__run_callback'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/base.rb:501:in `initialize'
from (irb):23:in `new'
from (irb):23
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p286 :024 >
Your problem is that update is a column in your model. But update is also a method defined by the Active Record. So rails won't know which your talking about if you try and access admin.update. You should be fine if you rename your update column to something else.
You might want to look at timestamps. http://guides.rubyonrails.org/migrations.html#special-helpers it looks to me like what you want to do with the update column.

Resources