是否可以activerecord通过generate scaffold命令在模型中建立双重关系?
例如,如果我有User模型和PrivateMessage模型,则pm表需要跟踪sender和recipient.
显然,对于单一的关系我会这样做:
ruby script/generate scaffold pm title:string content:string user:references
Run Code Online (Sandbox Code Playgroud)
是否有类似的方式来建立两个关系?
另外,有没有为关系设置别名?
所以不要说:
@message.user
Run Code Online (Sandbox Code Playgroud)
您可以使用以下内容:
@message.sender 要么 @message.recipient
任何建议将不胜感激.
谢谢.
我对急切加载和延迟加载感到困惑,rails查询的性能有什么不同吗?
有没有办法实现这两种方式?
假设我有一个类"ClassA",它依赖于类"ClassB"(注入到ClassA的构造函数中).我想模拟ClassB,以便我可以单独测试ClassA.这两个类都是内部的.
如果我错了,请纠正我,但看起来Moq只能模拟一个类,如果它是公共的,它有一个公共无参数构造函数,并且要模拟的方法是public virtual.我真的不想让这些课程公开可见.我错过了Moq的东西,还是不适合我想做的事情?
我想我可以创建一个ClassB实现的接口(比如"IClassB"),将其注入ClassA,然后模拟接口.ClassB仍然可以是内部的(虽然我意识到接口方法必须是公共的).虽然这可行,但我对创建大量接口感到不安,其唯一目的是支持单元测试模拟.思考?
我在理解其他SO问题中的含义方面没有取得多大成功.
这是一个ruby on rails项目.如果你知道该怎么做,它可能真的很简单.我尝试合并两个分支,这是结果的(部分).
CONFLICT (rename/delete): db/migrate/20160705073411_create_building_employees.rb deleted in HEAD and renamed in user-authentication. Version user-authentication of db/migrate/20160705073411_create_building_employees.rb left in tree.
Removing app/models/buildings_user.rb
Automatic merge failed; fix conflicts and then commit the result.
Run Code Online (Sandbox Code Playgroud)
由他们添加:db/migrate/20160705073411_create_building_employees.rb
当我打开文件时,我没有看到任何真正解决的问题.页面上没有运行asterix.
任何建议将不胜感激.
一个棘手的(令人沮丧的问题) - 也许你们大家可能很聪明地解决它:
问题
我希望能够使用Entity Frameworks读取/写入我的数据库.我有一个在Heroku(简单的脚手架)上运行的简单app rails.我想连接到这个数据库并操纵记录.好消息是我可以使用npgsql成功连接到该数据库.坏消息是我不能用实体框架来做.这是我收到的错误:
System.Data.Entity.Core.ProviderIncompatibleException:从数据库获取提供程序信息时发生错误.这可能是由实体框架使用不正确的连接字符串引起的.检查内部异常以获取详细信息,并确保连接字符串正确.---> System.Data.Entity.Core.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串.---> System.IO.FileLoadException:无法加载文件或程序集'Npgsql,Version = 3.1.2.0,Culture = neutral,PublicKeyToken = 5d8b90d52f46fda7'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
这是堆栈跟踪:
at Npgsql.NpgsqlServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
--- End of inner exception stack trace ---
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
--- End of inner exception stack trace ---
at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at …Run Code Online (Sandbox Code Playgroud)