I see ActiveAdmin has a feature called comments:
https://activeadmin.info/1-general-configuration.html#comments
I want to play around with this feature. So I enabled it, ie
config.comments = true
config.comments_registration_name = 'ActiveAdminComment'
When I restarted my rails server, I got error message:
error='ActionView::Template::Error: PG::UndefinedTable: ERROR: relation "active_admin_comments" does not exist
Clearly I need to create a table called active_admin_comments.
But how do I create this table? Can ActiveAdmin generate a migration to create this table?
Thanks
Yes. The install generator should create the migration for you. If you don't have it try re-running the installation, perhaps on a new project. If you look in the source you will find it in lib/generators/active_admin/install
Related
I've just started programming using Instant rails and don't know much about it. So, I've made a rails project and created a database name contactlist_development and a table named contacts with some fields. I've generated a model named Contact and started the rails console using "ruby script/console".
But when i tried making a new Contact model object, it displays StatementInvalid : could not find the table 'contacts'.
Running rake db:migrate will execute the code you generated, hence creating the table.
Hope this helps!
Have you used rails generate model contact command to make Contact model? If yes, you forget to run command :
rake db:migrate
Or you can make table directly by creating migration or using sql query.
I was attempting to add a tagging model to my rails blog. But I accidentally generated a "Tags.rb" model as opposed to "Tag.rb", after reading the guide I realized that making "tag" plural was a mistake when it comes to model. I rolled back the migration using
rake db:rollback
and then
rails destroy model Tags.rb
and this was what i got back
invoke active_record
remove /home/migration/templates/create_table_migration.rb
remove app/models/tags.rb.rb
invoke test_unit
remove test/models/tags.rb_test.rb
remove test/fixtures/tags.rbs.yml
When I come back to the model folder though its still there.
Please help :)
Just give
rails destroy model Tag
Try using tags plural without the additional .rb, like
rails destroy model Tags
Hope this helps
You can manually delete the files, or just manually remove the "s"s.
try this
bundle exec rake db:rollback
and then delete your model as
rails destroy model <model_name>
now totaly deleted.
You can delete the migration files in migrate folder and model in your model folder and then you can again create new one which you wants.
you have to run that commands
rails g model tag
Then migrate database
rake db:migrate
First off, when generating a Rails model using rails generate model, don't add .rb to the end of the model name. Rails automatically does that so if you add .rb yourself then you end up with a model named Tag.rb.rb, which is invalid.
You're running the remove generator with "Tags.rb". Did you generate the model originally using rails generate model Tags.rb? If you did, then running rails destroy model Tags.rb will hopefully remove all the files, but since the model name is invalid, it might not work as expected (your use case is outside the intended use case of the generator). If you originally generated the model using rails generate model Tags, then running rails destroy model Tags should work.
If you originally invoked rails generate model Tags.rb, and running the remove command isn't working (which again, in understandable given that Tags.rb is an invalid name--would have been nice if the generator had originally prohibited you from generating a Tags.rb.rb file, but you can't have everything):
If you can afford to destroy your database and rerun all the migrations, the easy solution will be to manually remove the added files, destroy the database, and rerun all migrations. Then you can add the correct model using rails generate model Tag.
If you can't afford to destroy your database, then you'll need to manually find the problem migration in the migration's folder, run the down command on that migration, and then manually delete that migration file and all the other files.
I am trying to access all records of a table that has an underscore in its name.
For example if I have table in my schema that is called trips I can do Trip.all in rails console. But what do I do if my table name contains an underscore (e.g. users_foods)
I tried the following options:
Users_food.all
User_food.all
User_foods.all
etc.
All of the above did not work, any suggestions?
Figured it out
One can access the data with UserFood
For a table named users_foods, ActiveModel should provide you with a corresponding Rails model UsersFood, to fit Ruby/Rails object naming convention. Try that.
Class names Users_food or User_food, etc. do not conform to Ruby convention.
I had the same problem when creating bonus_histories table. And didn't work any of the answers until I found out why.
I have made only rails g migration BonusHistory
and it was the problem. My rails console didn't find BonusHistory at all because I had no model.
So I had to first rollback the migration rake db:rollback STEP=1, then deleted the migration file and finally made
rails g model BonusHistory
and after migrating that table, when I enter rails console, I can successfully ask for BonusHistory.count
I have two models in my rails application. One is User that I have generated through with Devise and another one is Links. Now Initially There was only the Link model in my application. So I had two columns in my model Link namely "id" and "link" . Now my requirements have changed and I need implement a User model. A user in my application has one or many links. So added the line has_many :links to my "user.rb" and belongs_to :users to my "link.rb" file.
And I generated a migration like:
rails generate migration add_user_id_to_link user:references
That is, I'm generating a migration file to modify my existing table "links" and add a reference column "user_id" to the table.
But it's giving an error when running rake db:migrate. Then I ran the command:
rails genearate migration add_user_id_to_links user_id:integer
But it's giving a command not found error. Now the question is how do I achieve this??
Thanks in advance...
I'm pretty sure the references helper only works for create_table. Which is why the first command doesn't work.
The second command should have worked though. The error is perhaps pointing to your typo in generate.
I hope that helps.
You can do rails g migration add_user_id_to_links user_id:integer:index if you want to assign each user to a link that they have posted.
I'm trying to generate a Post model in rails using the following code:
rails g model Post url:string, title: string
When I execute that line I get the following output:
invoke active_record
Another migration is already named create_posts: /Users/myname/Desktop/folder/my_project/db/migrate/20121212021831_create_posts.rb
It seems to be expressing a conflict as though the file already exists in my Model folder - which it does not.
Is this a naming problem? Any thoughts?
The conflicting migration would be in your db/migrate folder, not app/models.
Your two options are naming your new migration something else or removing the old migration. If you choose to remove the old migration, make sure to roll it back first before deleting it, so that your database schema is correct.
This problem can happen when you execute rails g model post for multiple times. You can resolve this issue by executing the rails destroy model post to remove last generated content.