草庐IT

filter_has_var

全部标签

ruby-on-rails - Rails 模型 has_many , belongs_to 关系

我有两个模型classUser我需要在Product表中添加一列user_id还是Rails默认添加它? 最佳答案 您需要手动将user_id列添加到Product模型。如果您尚未创建模型,请将列列表中的引用添加到模型生成器。例如:rails生成模型Productname:stringprice:decimaluser:references或者,如果您的Product模型已经存在,您需要做的是:railsgmigrationaddUserIdToProductsuser_id:integer这将生成一个迁移,将user_id列正确添

ruby-on-rails - 一个 has_many :through Relationship 的 Rails RSpec 测试

我是测试和Rails的新手,但我正努力让我的TDD流程正常运行。我想知道您是否使用任何类型的范例来测试has_many:through关系?(或者我想一般来说只是has_many)。例如,我发现在我的模型规范中,我肯定会编写简单的测试来检查关系两端的相关方法。即:require'spec_helper'describePostdobefore(:each)do@attr={:subject=>"f00PostSubject",:content=>"8arPostBodyContent"}enddescribe"validations"do...enddescribe"categoriz

ruby - 导轨 4 : Append to a "has_many" relation without saving to DB

在Rails3中,可以做类似some_post.comments.append(some_comment)的事情其中一些帖子是“有很多”评论的模型实例。我在Rails4中面临的问题是append方法现在保存到数据库(如push和),我只需要“附加”而不将附加的对象保存到数据库。我们如何在Rails4中实现这一点?我不能使用some_post.comments.build(some_comment.attributes)因为我需要保留some_comment中已经存在的其他关系实例。 最佳答案 在Rails中优雅地做到这一点非常困难。

ruby-on-rails - 如何避免 has_many :through relationship? 中的重复项

我怎样才能实现以下目标?我有两个模型(博客和阅读器)和一个JOIN表,它允许我在它们之间建立N:M关系:classBlog:destroyhas_many:readers,:through=>:blogs_readersendclassReader:destroyhas_many:blogs,:through=>:blogs_readersendclassBlogsReaders我现在想做的是将读者添加到不同的博客。不过,条件是我只能将读者添加到博客一次。因此BlogsReaders表中不能有任何重复项(相同的readerID,相同的blogID)。我怎样才能做到这一点?第二个问题是,

ruby-on-rails - Rails before_filter 用于 Controller 中的特定操作

defnewbefore_filterdoredirect_to"/"unlesscurrent_admin||current_companyflash[:notice]='Youdonthaveenoughpermissionstobehere'unlesscurrent_admin||current_companyendCODECODECODEenddefeditbefore_filterdoredirect_to"/"unlesscurrent_admin.id=5flash[:notice]='Youdonthaveenoughpermissionstobehere'unles

ruby-on-rails - Rails 生成 has_many 关联

有没有办法在控制台中使用Railsgeneratescaffold命令为列生成has_many关联?我知道belongs_to可用并且有references的用例但不确定has_many 最佳答案 has_many关系没有列。belongs_to由包含外键的列支持。所以如果你生成一个脚手架:railsgscaffoldPost然后生成另一个脚手架:railsgscaffoldCommentpost:references然后rails将创建一个迁移,将名为post_id的列添加到Comment表并在其上创建索引。对于这两个表,它在co

ruby-on-rails - 在 where 查询中查找 nil has_one 关联

这可能是一个简单的问题,但我似乎正在努力寻找一个优雅的解决方案。我有两个ActiveRecord模型类,它们之间有has_one和belongs_to关联:classItem我正在寻找一种优雅的方法来查找所有Item对象,这些对象没有与之关联的购买对象,理想情况下无需求助于boolis_purchased或Item上的类似属性。现在我有:purchases=Purchase.allItem.where('idnotin(?)',purchases.map(&:item_id))这行得通,但对我来说似乎效率低下,因为它执行两个查询(并且购买可能是一个庞大的记录集)。运行Rails3.1.

使用 Ubuntu 的 Ruby gem 权限被拒绝/var/lib/gems

我正在尝试使用Gem安装Rails并从终端执行以下操作:sudoapt-getinstallruby-fullbuild-essentialsudoapt-getinstallrubygemsexportPATH=/var/lib/gems/1.8/bin:$PATHgeminstallrails然后我收到以下消息:ERROR:Whileexecutinggem...(Errno:EACCES)Permissiondenied-/var/lib/gems我已按照UbuntuHelpGuide中的说明进行操作&我正在使用OracleVMVirtualBox在Windows7下运行Ubun

ruby-on-rails - Rails 模型 has_many 和多个 foreign_keys

相对较新的Rails并尝试使用具有名称、性别、father_id和mother_id(2个parent)的单个Person模型来建模一个非常简单的家庭“树”。下面基本上是我想做的,但显然我不能在has_many中重复:children(第一个被覆盖)。classPerson'Person'belongs_to:mother,:class_name=>'Person'has_many:children,:class_name=>'Person',:foreign_key=>'mother_id'has_many:children,:class_name=>'Person',:foreig

ruby-on-rails - 为什么我在安装 PaperClip 时得到一个 "undefined method for ` has_attached_file`?

我刚刚安装了Paperclip插件,但收到以下错误消息,但我不确定原因:NoMethodError(undefinedmethod`has_attached_file'for#):/Users/bgadoci/.gem/ruby/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in`method_missing'app/models/post.rb:2app/controllers/posts_controller.rb:50:in`show'它引用了will_paginategem。据我所知,我的PostsC