Rails 3.0.9 Model attributes are lost on database query - ruby-on-rails

If i open the console and type in the following code, my model looses attributes. In all cases only the first attribute after the id is accessable. I had this problem on one server. My old server is working fine with the same code and same versions. Here the output:
irb(main):001:0> User.new
=> #<User id: nil, encrypted_uid: nil, encrypted_access_token: nil, created_at: nil, updated_at: nil>
irb(main):002:0> User.first
=> #<User id: 1, encrypted_uid: "I7lPHOYoGMNWki3cZtb5oA==\n">
ActiveModel::MissingAttributeError (missing attribute: encrypted_access_token):
Has anyone an idea to get it working? Thanks in advance.

I had to recreate the application and copy the model into hte new application. after that everything worked fine. No clue what was wrong.

Related

Undefined method 'unshift'

I'm creating a new Rails app with Rails 6 and I want to do something like this:
#items = Item.all.unshift Item.new(id: 0, name: '-- Kein Item --')
But I get the error "undefined method `unshift' for #<ActiveRecord::Relation [#<Item id: 1, name: "Item 1", created_at: "2021-03-13 11:09:34.284978000 +0000", updated_at: "2021-03-13 11:09:34.284978000 +0000">]>"
In another Rails app I can do it, but that app is running under Rails 4.
I want to add this "empty" record so that model project hast not everytime an item and because I have somewhere Project.item.name it won't give an nil error.
Any suggestions?

Paperclip and Zip - My .zip file is magically transforming into a .png

I'm having a problem with Paperclip. Easily enough, I am trying to get a .zip folder uploading using Paperclip - I've read around and it seems like it can be done.
However, my .zip uploads transform into the default 'missing.png' file - and I have no earthly idea why. The file is, er... well, the process is going through and I am not getting any errors, but obviously something is not turning out right.
I've followed the instructions of Can't upload zip files using ruby on rails and paperclip gem but it's still not... changing anything.
From what I can tell through a rails console run of the model in question, the file isn't uploading at all. The problem is, I'm not sure where or how or why.
Here's my book model
book.rb
has_attached_file :content
validates_attachment_content_type :content, :content_type => ["application/zip, application/x-zip"]
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(attachment_content_type)
end
end
And a quick rails console peek
2.1.6 :001 > Book.all
Book Load (0.7ms) SELECT "books".* FROM "books"
=> #<ActiveRecord::Relation [#<Book id: 3, title: "Test", summary: "La la la", rating: nil, author_id: nil, word_count: nil, created_at: "2016-10-28 03:53:22", updated_at: "2016-10-28 03:53:22", cover_file_name: nil, cover_content_type: nil, cover_file_size: nil, cover_updated_at: nil, content_file_name: nil, content_content_type: nil, content_file_size: nil, content_updated_at: nil>, #<Book id: 4, title: "ugh", summary: "", rating: nil, author_id: nil, word_count: nil, created_at: "2016-10-28 03:59:33", updated_at: "2016-10-28 03:59:33", cover_file_name: nil, cover_content_type: nil, cover_file_size: nil, cover_updated_at: nil, content_file_name: nil, content_content_type: nil, content_file_size: nil, content_updated_at: nil>]>
2.1.6 :002 >
Ignore the 'cover' section, there was a separate Paperclip attachment that I hid to diagnose the 'content' problem.
But note, I'm not getting ANY errors. At all. Whatsoever. So. Any help from the StackOverflow Rails Gawds would be greatly appreciated for a newb like me.
Solved!
As #henners66 suggested, I was completely missing the :content as a permitted param on my books controller - further, turns out I was declaring the 'valid content types' incorrectly. It should have been:
book.rb
has_attached_file :content
validates_attachment_content_type :content, :content_type => ["application/zip", "application/x-zip"]
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(attachment_content_type)
end
end
Thanks everyone!

Migrate models between different environments in Rails

I am writing a script that will be in charge of transferring data from production db to a new production db.
There is one Rails app connecting to each database. In the new app we have made some migrations that change the schema (remove columns, etc)
I tried to do this:
rails console old
2.0.0p247 :004 > tag = Tag.last.dup
=> #<Tag id: nil, description: " views", account_id: 46, screenshotBase64: "", user_id: 1, created_at: nil, updated_at: nil>
2.0.0p247 :005 > ActiveRecord::Base.establish_connection(:development)
2.0.0p247 :006 > tag.save
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'screenshotBase64' in 'field list
As we removed the screenshotBase64 in the new db, it does not work.
Is there a way to do that with Rails ? Delete attributes from Rails model before save ?
Is there a better way to transfer all data from a user linked through its associations between two databases (changing ids in the new db) ?
Please have a try with this code
tag_attributes = Tag.last.attributes.dup
%w(id screenshotBase64).map{|method| tag_attributes.delete method}
ActiveRecord::Base.establish_connection(:development)
tag = Tag.create(tag_attributes)

rspec not saving to database

I'm trying to test the login method below, but it does not seem to be saving the data_provider_user to the database. I've checked using debugger and it works perfectly until it leaves the method and goes back to the rspec code, as shown below. I've also checked to see if the object is valid after it save using .valid? and it is so I'm a bit lost!
def login
require 'TwitterOauth'
#data_provider_user = DataProviderUser.find(params[:id])
if #data_provider_user.twitter?
#request_token = TwitterOauth.request_url
#data_provider_user.access_token = #request_token.token
#data_provider_user.oauth_token_secret = #request_token.secret
if #data_provider_user.save
debugger
redirect_to #request_token.authorize_url
end
end
end
Test:
it "should update the tokens" do
require 'TwitterOauth'
TwitterOauth.stub(:request_url).and_return(#token)
debugger
get :login, {id: #data_provider_user.id}
debugger
#data_provider_user.access_token.should_not eq(nil)
#data_provider_user.oauth_token_secret.should_not eq(nil)
end
Debugger output:
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:22:12", access_token: "186553918-sEAEO2fcvtyO1x99eH4Q4XwVcOYatODCQ5f1TwqD", update_frequency: nil, oauth_token_secret: "gLw2PtUyTZfxIan1gJBnbP7icboXbi98KlUoOn7ycVs">
#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>
Any help would be greatly appreciated!!
------ UPDATE -------
There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:
#data_provider_user.reload
before hand which refreshes the object, updating the values accordingly.
There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:
#data_provider_user.reload
before hand which refreshes the object, updating the values accordingly.
In spec/rails_helper.rb
Set false to use_transactional_fixtures:
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
Are you sure you're looking at the right database? When you're doing testing it's probably going to myapp_test (myapp being whatever you called your app). You may be looking at your myapp_development database.
If you're accessing the database via rails db chances are you're going into your development database. You can check with select database(); if you're using MySQL. If you want to check your test database, then use RAILS_ENV=test rails db. Again, you can use the select statement to verify you're in the right database.

Error with "to_sql" on Rails 3 Beta 4

I'm testing Rails 3 beta 4 on Ruby 1.9.2-head, and when I start a
console and do:
Game.first.to_sql
I get this error:
ArgumentError: wrong number of arguments (0 for 1)
I know it can find the Game record, because when I type:
Game.first
it returns:
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37",
updated_at: "2010-06-19 11:02:37">
What am I missing? I just want to make the to_sql work in a very simple
case.
.
When you run Game.first you are returning a Game object, not a ActiveRecord::Relation object as you are expecting.
To do what you're trying to do, you'll need to do:
Game.limit(1).to_sql
This lets you run it without to_sql and return the object as you expected it, although it will be in an array, which then you can run .first on it like you wanted anyways.
irb(main):004:0> Game.limit(1).to_sql
=> "SELECT `games`.* FROM `games` LIMIT 1"
irb(main):005:0> Game.limit(1).class
=> ActiveRecord::Relation
irb(main):006:0> Game.limit(1)
=> [#<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">]
irb(main):007:0> Game.limit(1).first
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">
When you dig into the source, when you run .first on an ActiveRecord::Relation it runs the following (which is the same as I showed you):
def find_first
if loaded?
#records.first
else
#first ||= limit(1).to_a[0]
end
end

Resources