Tag: 关联

Rails:如何限制has_many关联中的项目数(来自Parent)

我想限制关联中的项目数。 我想确保用户没有超过X物品。 这个问题之前被问过 ,解决方案在孩子身上有逻辑: 提供的解决方案(针对类似问题): class User :destroy end class Thing :create def thing_count_within_limit if self.user.things(:reload).count >= 5 errors.add(:base, “Exceeded thing limit”) end end end 硬编码的“5”是一个问题。 我的限制根据父级更改。 物品集合知道它相对于用户的限制。 在我们的例子中,管理员可以调整每个用户的限制(事物),因此用户必须限制其收集的东西。 我们可以让thing_count_within_limit请求其用户的限制: if self.user.things(:reload).count >= self.user.thing_limit 但是,这是Thing的很多用户反省。 对用户的多次调用,特别是(:reload)对我来说是红色标志。 想要更合适的解决方案: 我认为has_many :things, :before_add => :limit_things会起作用,但我们必须提出一个例外来阻止链 。 这迫使我更新things_controller以处理exception,而不是if valid?的rails约定if valid? 或者if save 。 class User has_many :things, :before_add => […]

有和属于许多错误 – * _development.hunts_tasks’不存在:

所以我正在开展一个项目,其中有一些任务组成了寻宝者。 因此,当用户点击特定的搜索时,我希望show.html.erb文件显示搜索以及与该搜索相关的任务。 两个模型(Hunt.rb和Task.rb)彼此之间具有“has_and_belongs_to_many”关系。 我的控制器最初有这个代码,但它显示了数据库中的每个任务,而不仅仅是与特定搜索相关的任务。 def show @hunt = Hunt.find(params[:id]) @title = @hunt.name @tasks = Task.paginate(:page => params[:page]) end 所以我尝试了这个。 def show @hunt = Hunt.find(params[:id]) @title = @hunt.name @tasks = @hunt.tasks.paginate(:page => params[:page]) end 但后来它抛出了这个错误: ActiveRecord::StatementInvalid in Hunts#show Showing /****/views/hunts/show.html.erb where line #10 raised: Mysql2::Error: Table ‘***_development.hunts_tasks’ doesn’t exist: SELECT `tasks`.* FROM `tasks` INNER JOIN `hunts_tasks` […]

Rails在两个表上自定义foreign_key名称

我有两个模型,例如User和Club及其属性: User: id uid email etc. 和 Club: id player_id address supporter etc. 出于某种原因,join属性是带有users.uid clubs.player_id , users.uid不是带有users.id 。 是否可以使用has_one和belongs_to将这两个模型与one-to-one关联连接起来? 谢谢

Rails Polymorphic has_many

使用Ruby on Rails,我如何实现多态has_many关系,其中所有者始终是已知的,但关联中的项将是某些多态(但同质)类型,由所有者中的列指定? 例如,假设Producer类has_many产品,但生产者实例可能实际上有许多Bicycles,或Popsicles或Shoelaces。 我可以轻松地让每个产品类(Bicycle,Popsicle等)与Producer有一个belongs_to关系,但是给定一个生产者实例,如果它们的类型不同(每个生产者实例),我如何获得产品集合? Rails多态关联允许生产者属于许多产品,但我需要这种关系是另一种方式。 例如: class Bicycle < ActiveRecord::Base belongs_to :producer end class Popsicle < ActiveRecord::Base belongs_to :producer end class Producer :type # last part is made-up… end 所以我的Producer表已经有一个“类型”列,它对应于某个产品类(例如Bicycle,Popsicle等)但是我如何让Rails让我做一些像: >> bike_producer.products #=> [Bicycle@123, Bicycle@456, …] >> popsicle_producer.products #=> [Popsicle@321, Popsicle@654, …] 对不起,如果这是显而易见的或常见的重复; 我很难轻易实现它。

Rails:一次保存更新记录的集合

据我了解, build方法可用于在保存之前建立关联记录的集合。 然后,当调用save ,将validation并保存所有子记录,如果存在validation错误,则父记录将出现反映此错误的错误。 第一个问题是,这是正确的吗? 但我的主要问题是,假设上述内容有效,是否可以对更新执行相同的操作,而不是创建? 换句话说,是否有一种方法可以更新与父记录关联的集合中的多个记录,然后保存父记录并立即进行所有更新(如果子级中存在validation错误,则会在父级中出现错误) )? 编辑:总而言之,我想知道正确的方法来处理父记录和几个相关子记录需要一次更新和保存的情况,任何错误都会中止整个保存过程。

has_many:通过外键?

我已经阅读了多个有关此问题的问题,但尚未找到适合我情况的答案。 我有3个型号: Apps , AppsGenres和Genres 以下是每个相关领域: Apps application_id AppsGenres genre_id application_id Genres genre_id 这里的关键是我没有使用那些模型中的id字段。 我需要根据那些application_id和genre_id字段关联表。 这是我目前得到的,但它没有得到我需要的查询: class Genre :application_id, :foreign_key => :application_id has_many :apps, :through => :apps_genres end class AppsGenre :application_id belongs_to :genre, :foreign_key => :application_id, :primary_key => :application_id end class App :application_id, :primary_key => :application_id has_many :genres, :through => :apps_genres end 作为参考,这是我最终需要的查询: @apps = […]

validation失败类必须存在

我在Rails中遇到了很多关联的问题。 我发现了很多类似的问题,但我不能申请我的案子: 城市class级: class City < ApplicationRecord has_many :users end 用户class级: class User < ApplicationRecord belongs_to :city validates :name, presence: true, length: { maximum: 80 } validates :city_id, presence: true end 用户控制器: def create Rails.logger.debug user_params.inspect @user = User.new(user_params) if @user.save! flash[:success] = “Works!” redirect_to ‘/index’ else render ‘new’ end end def user_params params.require(:user).permit(:name, :citys_id) […]

Rails has_one vs belongs_to语义

我有一个模型表示包含一些图像的Content项。 图像的数量是固定的,因为这些图像参考非常特定于内容。 例如, Content模型两次引用Image模型(轮廓图像和背景图像)。 我试图避免genericshas_many ,并坚持多个has_one的。 当前的数据库结构如下所示: contents – id:integer – integer:profile_image_id – integer:background_image_id images – integer:id – string:filename – integer:content_id 我在这里无法弄清楚如何正确设置关联。 Content模型可以包含对Image两个belongs_to引用,但是在语义上似乎不正确,因为理想情况下图像属于内容,或者换句话说,内容具有两个图像。 这是我能想到的最好的(通过打破语义): class Content belongs_to :profile_image, :class_name => ‘Image’, :foreign_key => ‘profile_image_id’ belongs_to :background_image, :class_name => ‘Image’, :foreign_key => ‘background_image_id’ end 我离开了,有更好的方法来实现这种关联吗?

rails scope来检查关联是否不存在

我期待编写一个范围,返回所有没有特定关联的记录。 foo.rb class Foo < ActiveRecord::Base has_many :bars end bar.rb class Bar < ActiveRecord::Base belongs_to :foo end 我想要一个范围,可以找到所有没有任何bars的Foo’s 。 很容易找到使用joins的关联,但我没有找到相反的方法。

Rails嵌套属性关联validation失败

我有一个Rails模型的嵌套属性,并且关联validation由于某种原因失败。 我没有使用accepts_nested_attributes_for,但我正在做一些非常相似的事情。 class Project “name”, :value=>val) end end end class ProjectAttribute :project_id validates_presence_of :project_id, :unless => lambda {|attribute| attribute.project.try(:valid?)} validates_associated :project end 这是一个人为的例子,但与我试图做的类似。 我已经看了一下accepts_nested_attributes_for做了什么,它使用了构建方法,我认为它会将构建的属性与项目相关联。 我还查看了accepts_nested_attributes_for子关联validation失败 ,它给了我validates_presence_of :unless=>valid? 有关如何使其工作的任何想法?