草庐IT

has-many-through

全部标签

ruby-on-rails - 回形针 gem - "Image has contents that are not what they are reported to be"错误

该网站的功能是发布博客文章。它在Windows7上本地运行。我试过Paperclipgem(版本4.2.4和4.3)并且服务器在cmd中进入无限循环(在4.2.4上没有发生但仍然出现错误)。我做了bundleinstall并且它确实安装了。gem文件:gem"paperclip","~>4.3"这是模型:classPost":style/rails1.jpg"validates_attachment_content_type:image,:content_type=>/\Aimage\/.*\Z/end这是我在尝试提交图像(png或jpg)时遇到的错误:Imagehascontents

ruby-on-rails - 如何获取具有 min has_many 记录的记录(加入数据)

用户.rbhas_many:properties属性.rbbelongs_to:user我想获得一个具有最小属性的用户,例如wiseformax。我找不到任何相关的查询 最佳答案 要找到具有min属性的用户,您可以简单地做,User.joins(:properties).group("properties.user_id").order("count(properties.user_id)desc").last并找到具有max属性的用户,User.joins(:properties).group("properties.user_i

ruby-on-rails - 如何在 Rails ActiveRecord 中正确分配 has_many/belongs_to 关系?

我有一个带有文章和作者模型的Rails应用程序。如果我有一个作者和一个帖子,并且想表明作者应该是文章的所有者,或者文章属于作者,那么这样做的最佳做法是什么?特别是:如果我设置了会有什么不同my_article.author_id=author_one.id或者如果我这样做author_one使用的关联是作者有_多篇文章文章属于作者另外,如果出现类似问题,最好的查找方式是什么? 最佳答案 以下3个没有区别:my_article.author_id=author_one.idmy_article.save#sameasmy_articl

ruby-on-rails - Rails,has_many :through, 未定义方法 `to_sym` 为 nil:NilClass

我一直在学习Rails,但在关系方面遇到了一个问题。我有多对多关联用户-锦标赛,由于某种原因,我无法访问用户实例上的participated_tournaments,或者无法访问锦标赛实例上的参与者。2.0.0-p643:001>Tournament.new.participantsNoMethodError:undefinedmethod`to_sym'fornil:NilClassfrom/home/marcin/.rvm/gems/ruby-2.0.0-p643/gems/activerecord-4.1.8/lib/active_record/reflection.rb:100

ruby-on-rails - 我如何在 Rails 中测试 belongs_to 和 has_many?

我正在使用rspec并尝试测试我的模型y是否有很多x。我尝试了各种方法,包括遍历方法数组,但似乎无法在网上找到好的方法。那我应该怎么用呢? 最佳答案 无需太多黑客攻击,您就可以使用卓越的gem:http://github.com/carlosbrando/remarkable摘自非凡的文档:describePostdoit{shouldbelong_to(:user)}it{shouldhave_many(:comments)}it{shouldhave_and_belong_to_many(:tags)}end

ruby-on-rails - Ruby on Rails 在保存之前通过关联对象 has_many

在RubyonRails项目中,我试图在将所有内容保存到数据库之前访问ActiveRecord上的关联对象。classPurchase如果我像这样构建我的对象:purchase=Purchase.new(item_ids:[1,2,3])并尝试保存它item_validation方法还没有填充项目集合,所以即使虽然项目已经设置,但它没有机会调用任何项目的check_something方法。是否可以在我的购买模型和关联模型保留之前访问项目集合,以便我可以对它们运行验证?如果我将item_validation方法更改为:defitem_validationpurchase_items.ea

ruby-on-rails - 通过关联获取 has_many 的 ActiveRecord::RecordInvalid 错误;连接表上的验证问题

我有如下三个关联模型:classProduct当我做类似的事情时:doc=user.documents.builddoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到false和doc上的相应错误。这正是我想要的。但是,如果doc已经存在,例如:doc=user.documents.firstdoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到一个ActiveRecord::RecordInvalid错误。我很清楚为什么会这样——i

ruby-on-rails - 设置一个 :has_many :through association on a belongs_to association Ruby on Rails

我有三个模型,每个模型都有以下关联:classModel1:model1#willthiswork?isthereanywayaroundthis?endclassModel3:model1#willthiswork?isthereanywayaroundthis?end正如您在评论文本中看到的那样,我已经提到了我需要的内容。 最佳答案 您只需创建访问它的方法classModel2或者,您可以将model3s方法委托(delegate)给model1classModel2:model1end

ruby - Rails 3 Order By Count on has_many :through

我有一个应用程序,我可以在其中列出项目并为每个项目添加标签。模型Items和Tags是这样关联的:classItem:taggingsendclassTagging:taggingsend因此,这种多对多关系允许我为每个Item设置n标签,并且同一个标签可以多次使用。我想列出所有标签,这些标签按与该标签关联的项目数排序。更多使用的标签,首先显示。较少使用,最后。我该怎么做?问候。 最佳答案 Tag.joins(:taggings).select('tags.*,count(tag_id)as"tag_count"').group(:

ruby-on-rails - mongoid中的has_many、has_and_belongs_to_many和embeds_many到底有什么区别?

我知道这不是编程问题,我找不到非常清晰和描述性的解决方案。 最佳答案 Mongoid的documentation很清楚:Embeddedrelationsdescribedocumentswhoarestoredinsideotherdocumentsinthedatabase.Referencedrelationsdescribedocumentsthatreferencedocumentsinanothercollectionbystoringforeignkeydata(usuallyanid)abouttheotherdoc