草庐IT

known_associations

全部标签

ruby-on-rails - `autodetect' : No known ORM was detected

无法使用database_cleaner.rb清理数据;在运行测试时抛出以下问题。/Users/prashanth_sams/.rvm/gems/ruby-2.0.0-p598/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:147:in`autodetect':NoknownORMwasdetected!IsActiveRecord,DataMapper,Sequel,MongoMapper,Mongoid,Moped,orCouchPotato,RedisorOhmloaded?(DatabaseCleaner::N

ruby-on-rails - rails 上的 ruby : Creating a model entry with a belongs_to association

我正在尝试在我的数据库中为具有belongs_to关系的模型添加一个新条目。我有2个模型,工作和客户。很容易找到有关如何在这两者之间建立关联(使用has_many和belongs_to)的教程,但我似乎找不到任何实际使用关联的示例。在我的代码中,我正在尝试为第一个客户创建一个新工作。jobs模型有一个client_id属性,我知道我可以手动填充该属性,但必须有一些ruby​​约定才能轻松完成此操作。Job.create(:client_id=>1,:subject=>"Test",:description=>"Thisisatest")我可以很容易地将它放入我的代码中,但我觉得ruby

ruby-on-rails - 如何使用不唯一的 "association_type"和 "association_id"在 Rails 中索引多态表

我有一个名为“投票”的多态表,其中包含来自答案和问题的投票。投票user_idvoteable_idvoteable_typevalue12Answer122Answer1在这种情况下,id=2的答案有两票赞成。问题是:如何索引这个表?第一种方法:add_index:votes,[:voteable_id,:voteable_type]这是行不通的,因为重复的键值会违反唯一约束第二种方法:add_index:votes,:voteable_id,add_index:votes,:voteable_type我猜这个不会有太多性能,因为同时对id和type进行复合查询。第三种方法:add_

ruby-on-rails - rails : Return only objects where all associations meet a condition

设置:Rails3.2.18、Postgres我有两个对象,例如,将它们称为Author和Article,具有以下设置:classAuthorhas_many:articles...endclassArticlebelongs_to:authorclass我正在尝试查找所有Author记录,其中所有相关的Article记录都是在一年前发布的。这段代码:Author.joins(:article).merge(Article.published_over_one_year_ago)...返回Author对象,其中至少一个关联的Article是在一年多以前发表的,但我只需要作者记录,其中所

ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked

我一直在努力弄清楚一些代码。我有一个表格,我正在尝试使用Wicked和茧gem。一切正常,包括link_to_add_association功能。正如Cocoon所建议的那样,我正在为关联的表单字段呈现部分内容,并且除了link_to_remove_association函数之外,一切似乎都正常工作。它返回以下错误:nil:NilClass的未定义方法new_record?这是我抛出错误的部分:这是调用部分的View:location%>这是调用View的Controller操作:classUserStepsController如果有帮助,这里是cocoon中抛出错误的函数:defli

ruby-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'

ruby-on-rails - 为什么我会出现间歇性的 Excon::Error::Socket: getaddrinfo: No address associated with hostname (SocketError)?

Rails4-Ruby2.2.2-亚马逊AWSS3-蜻蜓1.0.12-dragonfly-s3_data_store1.2-fog-aws0.10.0大约99%的时间我们没有问题。这个问题通常只发生在使用率很高的时候,但我注意到它也发生在几乎没有用户的时候。抛出错误的行:#excon/lib/excon/socket.rb#line100insidetheconnectionmethod.addrinfo=::Socket.getaddrinfo(*args)该错误在应用程序中无处不在。有时在没有远程连接时会出现错误。-我无法再验证这一点。我使用Rails记录器来捕获传入的参数,通过和

ruby-on-rails - rails : How to get has_many associations of a model

如何获取模型的has_many关联?例如,如果我有这个类:classA我想要这样的方法:A.get_has_many返回[B,C]这可能吗?谢谢! 最佳答案 您应该使用ActiveRecordreflections.然后你可以这样输入:A.reflect_on_all_associations.map{|assoc|assoc.name}这将返回你的数组[:B,:C] 关于ruby-on-rails-rails:Howtogethas_manyassociationsofamodel,我

ruby-on-rails - rails 4 : how to use named scope with has_many associations

在我的Rails4应用程序项目(模型)has_many视频(模型)中。我在视频模型中有一个命名范围:scope:live,where(is_deleted:0,sent_to_api:1)在我的一个项目View中,我这样做(项目是项目的一个实例):project.videos.live.size我希望得到的是那个特定项目中的项目数量,但我得到的是任何项目中的视频数量。就好像.live不是从.videos返回一个子集而是替换它。我看到它解释了here将命名范围相互链接起来应该与逻辑AND相结合,但是当应用于“关联方法”时[.videos在这种情况下的正确术语]似乎并不即将发生。正确的做法

ruby-on-rails - rails 4 : How to use includes() with where() to retrieve associated objects

我不知道如何使用.where()检索关联模型数据的方法。在此示例中,项目belongs_to用户...classProjectparams[:id]}).firstendend在App/views/projects/invite.html.erg返回:---!ruby/object:Projectattributes:id:22name:SomeProjectNamebelongs_to:1instructions:Blablablaactive:truemax_duration:2max_videos:created_at:2013-08-2615:56:50.000000000Zu