In my models I have an AssessmentItem which has_many :items_levels. When I try to import an ItemLevel I get the error
Error during import: AssessmentItem(#70286054976500) expected, got
AssessmentItem(#70286114743280)
(/Users/stoebelj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/association.rb:218:inraise_on_type_mismatch!')
As far as I know, the parent record exists and I am referencing it with the correct mapping key.
Can someone give me an idea of what this error means and what might be the culprit?
I's too having these problems.
Since, in development environment, each time you make changes to class and save it in any editor, the class is registered as a new class and hence the mismatch of class' object instances.
Stop rails server
Run it again
Reload your page
Now, try uploading again
Repeat each time you make any changes to your code.
Related
I'm working some legacy code right now in seeds.rb. The previous developer used a method like this to add rows to the tables:
things_holder.oldthings_add(name)
where:
oldthings.rb
belongs_to :things_holder
things_holder.rb
has_many :oldthings
I can manipulate the object they created and adjust the seeds of the models they'd created. However, when I try to do the same to a model that I created myself (newthings), I can't seem to make it work.
Instead I get:
undefined method: newthings_add
where does this things_add method come from? I don't see it in any of the oldthings.rb files
*_add is not a standard Rails / Active Record method, so it's either defined somewhere in your application, or it's provided by some other gem.
As #jvillian's commented, you may be able to things_holder.method(:oldthings_add).source_location to learn where the method is defined.
If that doesn't work, you could try passing a blatantly invalid value to the method (e.g. things_holder.oldthings_add(true)), and see where the backtrace points.
Am trying to learn ASP.NET MVC and search the internet in and out including SO. A lot of questions and answers about |DataDirectory| as where and how.
When debugging the site breaks:
"The model backing the 'ASPNETDBContext' context has changed since the database was created...".
Then I checked the path: |DataDirectory|ASPNETDBContext.sdf
string path = AppDomain.CurrentDomain.GetData( "DataDirectory" ).ToString();
It points to the App_Data of the project but no file there.
The DB is entirely empty so deleting the thing is all I need.
My actual question I need to be answered: How to fix this properly?
made a follow-up: And additionally: Where is that file!
The answer to your problem lies in reading Scott Guthrie's blog
For those who are seeing this exception:
"The model backing the 'Production' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance."
Here is what is going on and what to do about it:
When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:
Database.SetInitializer<Production>(null);
Hope this will help you resolve the issue.
I have rather weird problem with paperclip gem. You know it defines Attachment class inside itself. So the model with exactly the same name already exists in my project. As the result in some parts of the code i cant get access to my previous Attachment model.
I tried to write full name of my model class, but the result was very interesting (in console):
ActiveRecord::Base::Attachment
=> Paperclip::Attachment
I can get access to my Attachment model inside AttachmentController and by default it console but nowhere else.
Also i tried to create simple object from console without attached file.
a.errors.sort
[]
a.save
TypeError: can't dump anonymous class Class
As you see object a has no errors but throws error in save.
Finally my aim is to copy a collection of Attachment objects from one holder-object to another. I mean deep copy, so files should be copied too. If you have any suggestions about these points I'll appreciate.
You can always access your class via "::Attachment", but make sure you're using the latest version of Paperclip. There were some namespace collision bugs that were fixed.
Yesterday i was absolutely sertain that all migrations data for EF placed in classes, placed in my solution as nested from DbMigration. But today i was dig a slightly deeper(just try to fallback to old migration with enable data loss not with nu-get and visual studio, but with code())
DbMigrator fg = new DbMigrator(new Settings() { AutomaticDataLossEnabled = true});
fg.Update("MigrationName");
And get exception, smth like "string should be truncated", those means that migrator tried to update column from big to small MaxLength attribute. So, i had excluded migration that caused this update and move this changes to migration, those create tables. The error still was occured. I got to intellitrace and it said that those(deleted) migration still was called. Looking to requests told me things like this:
SELECT [Extent1].[MigrationId] AS [MigrationId] FROM [dbo].[__MigrationHistory] AS [Extent1]
Looking to a table __MigrationsHistory and get my deleted migration there with field model that contains crypted data(don't decrypt this yet) . I was realy shocked. Does this means that all code, have written in classes is just the fake and really executed code placed here? And does anyone know, how to work with this table, register projections of migration classes to it etc. Or the once way to work with migrations is nu-get console?
I am not fully sure what your primary question is, so I will first try to answer last part about __MigrationHistory table.
Code in classes is not fake, your code in classes is compiled and run.
This table, however, really contains your database model, but it is not encrypted, it is compressed. The reason why Migrations API needs to store your model, is to be able to compare it against your current actual model and track changes for you (for example when you add a new property it will be able to tell what property you added and to perform automatic db migration).
In previous version of EF there was an EdmMetadata table where hash of your model was stored, and EF was able to detect if you made some changes to model by comparing stored and current model hash value. New version when migrations are enabled stores entire model as compressed blob, so it can do diff between the model that was used to create database and current model you are using, and make automatic migrations accordingly.
You should not work directly with this table, it is automatically populated by migrations API, but nuget console is not the only way to do migrations, you can check this resource for some insights how to do it from code.
Now, regarding your question from question title (where they are stored?), migrations are stored in code, in a class inheriting from DbMigration class that migrations API creates for you when you do Add-Migration command in nuget console. When you perform an migration (Update-Database), either from nuget package manager console or from code, API will compare your current model with versions in __MigrationsHistory to find initial version (if you have not specified it) and perform all migrations in between initial and target version (if not specified otherwise target is latest version).
I'm not really clear how you did exclude your migration that causes problems, as you need to migrate your database to version before that migration, and then delete and recreate all subsequent migrations from there.
Maybe you could solve your fallback to old version problem by implementing public override void Down() method in your migration that causes problems when trying to rollback? This method can be used to execute code which performs inverse any operations for migration.
Not directly related to question but worth mentioning, there is also pretty detailed tutorial here for EF CF.
Anyone have a workaround (without monkey patching rails) for the failure of the db:schema:dump part of a simple migration accessing the wrong database for a table? I get the error
*Mysql::Error: View 'database1.boxscores' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them: SHOW FIELDS FROM boxscores
*
When boxscores is on database2. The ActiveRecord model is set to make the connection and the app works fine, it isjust the migration that fails, and that only b/c of the schema dump.
I am going to answer this since I found the answer and it might help someone. No one else answered, so here we go:
By adding an Active Record connect I was able to solve this issue. It also turns out that the models for those tables were not overriding the default DB connection like they should have been.