ruby on rails rollback database created problems - ruby-on-rails

I completely rolled back my migration from the beginning so I can add a few columns, now when I'm trying to create new user I get a
NoMethodError in Devise::SessionsController#create
undefined method `image_changed?' for #<User:0x007fa99ea152f8>
I'm using devise and I have carrierwave. My application worked, and I had images before, but when I rolled back and deleted the images from the upload folder, I get this undefined method.
Does anyone know where this is coming from? I'm completely clueless,
Thanks

I believe that inside your user model you may have carrierwave still mounted to your model.
class User < ActiveRecord::Base
mount_uploader :image, ImageUploader
#mounts the uploader to the given column in this case which it is :image
end
The error undefined methodimage_changed?' for #` indicates that you must be missing some sort of column fro your model.
1) rails g migration AddImageToUsers image:string
2) Run rake db:migrate

Related

Rails couldn't find a valid model for ActiveStorage::Blob association, while trying to use activeadmin and active storage

I've:
Set up a rails project
Added Active Admin.
Set up Active Storage, updating the configuration to connect locally and doing:
bin/rails active_storage:install
bin/rails db:migrate
Then when trying to set up a very simple model Document with a file attached:
class Document < ApplicationRecord
has_one_attached :file
end
And the most simple Active Admin page possible for it:
ActiveAdmin.register Document do
permit_params :name, :type, :file
end
I'm getting the following error when trying to access the page:
Rails couldn't find a valid model for ActiveStorage::Blob association.
Please provide the :class_name option on the association declaration.
If :class_name is already provided, make sure it's an
ActiveRecord::Base subclass.
I'm unsure what is going wrong. It seems from the docs everything should work after the migration and adding that line to the model.
Ruby version: "3.1.1"
Rails version: "7.0.2"
I run into the same issue, and found out that this answer fixed the issue for me:
https://stackoverflow.com/a/72005456/583560
[...]adding an initializer with the following worked (Rails 6.0.4.7):
Rails.application.reloader.to_prepare do
ActiveStorage::Blob
end

I gat a error due to the influence of migration that I wrote a long time ago

I want to add new column 'piece_id' and I added it to Bucket table.
class AddPieceIdToBuckets < ActiveRecord::Migration
def change
add_column :buckets, :piece_id, :string
end
end
class Bucket < ActiveRecord::Base
validates :piece_id, presence: true
end
Also I need to validate piece_id. But Adding validation gives me the following error. I tried run migration without validation and then it passed.
undefined method `piece_id'
Although I do not have piece_id in migration I wrote in old days, I think that it is the influence that writing validation in the model. And I created several objects for migration I wrote before. Should I edit it?
def change
Bucket.reset_column_information
Bucket.create(
name: 'test_name',
about: 'test',
)
end
I have been bitten by this many times, I no longer put any data changes, via models, in my migrations for this reason.
Since you can be confident that the old migration where you create a Bucket has already run you could remove that bit of code, or better still add piece_id to the attributes and give it a value so it passes when migrating from an empty database.
I now use rake db:seeds to create data, in db/seeds.rb I have:
require_relative "seeds/#{Rails.env}/all"
and a directory structure such as:
db/seeds/development/all.rb
db/seeds/production/all.rb
In development/all.rb I do require_relative '../production/all'.
I run rake db:seed as part of deployment, this works well, just have sure that the seeds are idempotent, so check the record does not exist already, or use active record methods like find_or_create_by.

NoMethodError: undefined method reset_sent_at hartle tutorial

I'm doing chapter 10 of enter link description here. I got the same error mentioned in enter link description here
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method `reset_sent_at=' for #<User:0xccd47c0>
app/models/user.rb:66:in `create_reset_digest'
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:17:in `block in <class:PasswordResetsTest>'
I try to do everything mentioned in the answer.
first thing I did was:
rails generate migration add_reset_to_users reset_digest:string reset_sent_at:datetime
and the answer was:
Another migration is already named add_reset_to_users:
so I'm sure I did the migration before.
This _add_reset_to_users.rb file in migrate folder.
class AddResetToUsers < ActiveRecord::Migration
def change
add_column :users, :reset_digest, :string
end
end
Then I try to restart my rails server.(I'm not sure if I'm doing it right) using
rails server
and then shutting down the server.
Non of them worked. I'm still getting the same error.
Another migration is already named add_reset_to_users:
You had already created the migration previously, as you noted, but this migration did not include the addition of the reset_sent_at column.
At this point the easiest thing to do would be to create a new migration to add the missing column.
rails generate migration add_reset_sent_at_to_users reset_sent_at:datetime

Error while rake db:seed, prefilling database. uninitialized constant Clients_courses

I want to prefill my database with the db/seeds.rb but got an error during execution.
The schema is loaded and setup perfectly. But when I will prefill the database with rake db:seed i got this error:
uninitialized constant Clients_courses
I have got the Table clients_courses in my database schema. So I've tried to prefill it like this:
Clients_courses.create(:client_id => 6, :course_id => 2)
What's wrong with this?
I've tried it with some other tables as well for example with the users table. But theres the same problem.
You should have corresponding model to manipulate data like this. For users table you usually have User model. In your case you either have ClientsCourses or ClientsCourse model, or, assuming you have :has_and_belongs_to_many association there is no such model.
And in this case, the only way to do what you want is to use existing ones(Client and Course):
Client.find(6).courses << Course.find(2)
Create a model ClientsCourse
ie in app/model/clients_course.rb
Inside the file add line below and save
class ClientsCourse < ActiveRecord::Base
end
Correct the line as below
ClientsCourse.create(:client_id => 6, :course_id => 2)
Hope that helps
your model name should be client_course.rb and a table named client_courses

Rails migration: Add two references to one table

I am having a hard time extending an existing model to include two references to a single table.
My current model is called Order and I need two references (payment_state and delivery_state) to the State table.
It is my understanding that I should run the following command:
rails g migration AddStatesToOrder payment_state:references delivery_state:references
However, the following line comes up in my schema.rb after running rake db:migrate:
# Could not dump table "orders" because of following NoMethodError
# undefined method `[]' for nil:NilClass
Any help would be greatly appreciated. Thanks!
You should do like this:
rails g migration AddStatesToOrder payment_state_id:integer delivery_state_id:integer
And define the relation like this in the model:
belongs_to :payment_state, class_name: State.model_name
belongs_to :delivery_state, class_name: State.model_name
Why using :integer instead of :references? Because the thing you want to do is not "conventional", it is a customized double-relation to the same model, and Rails cannot guess it for you.

Resources