I ran a migration on an Image model to add the column Position.
The schema is updated, the column has actually been added to the database, and is accessible through Rails console. I'm even calling an order by clause using that column in ActiveAdmin, and that doesn't grumble .
However when trying to access that attribute in the app, I'm presented with unknown attribute errors.
When trying to display the column in an ActiveAdmin index view, I get:
undefined method `position' for #<Image:0x007f8a3429be98>
It (position) has been added to attr_accessible too.
I've also run:
Image.connection.schema_cache.clear!
Image.reset_column_information
and that hasn't helped either.
Rails 3.2.12
After some searching and trying I was able to reproduce the error it was triggered by a before filter.
This post led me to the solution:
Undefined method "reorder" for #<Array:0xbc38600> using ActiveAdmin
when it helped please upvote the other comment.
Related
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
I have been chasing an issue down for a while now, and still cannot figure out what's happening. I am unable to edit documents made from my gem through normal persistence methods, like update or even just editing attributes and calling save.
For example, calling:
Scram::Policy.where(id: a.id).first.update!(priority: 12345)
Will not work at all (there are no errors, but the document has not updated). But the following will work fine:
Scram::Policy.collection.find( { "_id" => a.id } ).update_one( { "$set" => {"priority" => 12345}})
I am not sure what I'm doing wrong. Calling update and save on any other model works fine. The document in question is from my gem: https://github.com/skreem/scram/blob/master/lib/scram/app/models/policy.rb
I cannot edit its embedded documents either (targets). I have tried removing the store_in macro, and specifying exactly what class to use using inverse_of and class_name in a fake app to reimplement these classes: https://github.com/skreem/scram-implementation/blob/master/lib/scram/lib/scram/app/models/policy.rb
I've tried reimplementing the entire gem into a clean fake rails application: https://github.com/skreem/scram-implementation
Running these in rails console demonstrates how updating does not work:
https://gist.github.com/skreem/c70f9ddcc269e78015dd31c92917fafa
Is this an issue with mongoid concerning embedded documents, or is there some small intricacy I am missing in my code?
EDIT:
The issue continues if you run irb from the root of my gem (scram) and then run the following:
require "scram.rb"
Mongoid.load!('./spec/config/mongoid.yml', :test)
Scram::Policy.first.update!(priority: 32) #=> doesn't update the document at all
Scram::Policy.where(id: "58af256f366a3536f0d54a61").update(priority: 322) #=> works just fine
Oddly enough, the following doesn't work:
Scram::Policy.where(id: "58af256f366a3536f0d54a61").first.update(priority: 322)
It seems like first isn't retrieving what I want. Doing an equality comparison shows that the first document is equal to the first returned by the where query.
Well. As it turns out, you cannot call a field collection_name or else mongoid will ensure bad things happen to you. Just renaming the field solved all my issues. Here's the code within mongoid that was responsible for the collision: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/persistence_context.rb#L82
Here's the commit within my gem that fixed my issue: https://github.com/skreem/scram/commit/25995e955c235b24ac86d389dca59996fc60d822
Edit:
Make sure to update your Mongoid version if you have dealt with this issue and did not get any warnings! After creating an issue on the mongoid issue tracker, PersistenceContext was added to a list of prohibited methods. Now, attempting to use collection_name or collection as a field will cause mongoid to spit out a couple of warnings.
Fix commit: https://github.com/mongodb/mongoid/commit/6831518193321d2cb1642512432a19ec91f4b56d
I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.
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.
To begin with, I must say that I am VERY new to Ruby on Rails. I've only been working my way through it for about a week.
I've starting integrating my existing website onto a blog I made which uses two resources - posts and comments.
My problem resides with the posts resource. I made the posts resource using "generate scaffold", to give me the fields "Title" and "Content", however I would now like to add another field "Image", which takes a local url of an image to add to the blog.
I've gone through manually and added references to the new "Image" field, adding it as a string with the intention of using the string as a target in an image_tag when it need to be displayed.
However when I visit pages that use the form to add or edit posts, I'm given the following error:
NoMethodError in Posts#new
undefined method `image' for #<Post:0x39a4868>
Is there an automated, catch-all way of adding a new field to a pre-existing resource?
Any help would be appreciated.
You need to generate a migration.
rails g migration add_image_to_posts image:string
Which will generate a file in db/migrate
You can then run this migration with:
rake db:migrate
And you should now have an image field in your model.