Tag: has many

如果记录与数组中的任何ID具有相同的id,请选择记录

我无法找到正确的语法和运算符来检查记录是否与存储在Array中的任何ID具有相同的ID。 has any? function还是类似? @problem = Remedy.where([“LOWER(\”remedyName\”) LIKE?”, “%#{params[:searchremedy]}%”.downcase]).pluck(:id) @pretreatments = Treatment.joins(:remedies_treatment).where(:remedies_treatment_remedy_id == @problem) 我已将代码更新为: ids = Remedy.where([“LOWER(\”remedyName\”) LIKE?”, “%#{params[:searchremedy]}%”.downcase]).pluck(:id) @pretreatments = Treatment.joins(:remedies_treatments).where(remedies_treatments: { remedy_id: @problem }) 但是我现在得到的错误是: SELECT “treatments”.* FROM “treatments” INNER JOIN “remedies_treatments” ON “remedies_treatments”.”treatment_id” = “treatments”.”id” WHERE “remedies_treatment”.”remedy_id” IS NULL Completed 500 Internal Server Error in 157ms (ActiveRecord: 17.0ms) ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: […]

关于Rails中正确关联的问题

以这种情况为例。 你有3个型号: 诗人 – 代表一首诗的作者 诗 – 代表诗人写的一首诗 打印 – 表示包含诗人诗歌的任何类型的印刷出版物。 蝙蝠诗人和诗歌是显而易见的: 诗人有很多诗 诗歌belongs_to诗人 在处理Printing模型时,我会感到困惑。 在这种情况下,印刷在某种意义上属于诗人和诗。 你可以说一个诗人has_many印刷品或诗歌has_many印刷这是有道理的,但走向相反的路线是棘手的… 一些杂志或书籍从一位诗人那里打出5首诗的情况怎么样? 或者,其中一首诗出版在10种不同的杂志上? 几乎看起来印刷本身“属于许多”诗歌或诗人。 我知道这是错的,但我只想说清楚这一点。 所以可回答的问题是:你将如何建立这些关系? 具体来说,模型和数据库表的外观如何?如何使用它们来访问关联数据? 谢谢!

在has_many关联中为“no children”创建named_scope

我想为没有post的博客提供一个named_scope。 以下不起作用。 class Post < ActiveRecord::Base belongs_to :blog end class Blog “blogs.id NOT IN (SELECT blog_id FROM posts)” end

Rails4 has_many通过参数

我有一个has_many through协会如下: has_many :bookmarks, -> {where(‘actions.bookmark=true’)}, :through => :actions, :source => :object 我想要做的是扩展上面的内容以传递被加标签的对象的类别。 类似于以下内容: has_many :bookmarks, -> (category) {where(‘actions.bookmark=true and objects.category=?’, category)}, :through => :actions, :source => :object 我希望它能让我进行诸如user.bookmarks(“papers”) 。 但是,当我尝试以上操作时, category采用# (基本上是上面调用中的user ),我似乎无法用简单的字符串调用关联。 有关如何实现这一目标的任何建议? 非常感谢。

如何在Active Record中检索批量插入的已创建ID列表?

我有三个型号: class Coupon :destroy has_many :events, :through => :coupon_events end class Event :destroy has_many :coupons, :through => :coupon_events end class CouponEvent < ActiveRecord::Base belongs_to :coupon belongs_to :event end 我通读了一个CSV文件来创建优惠券和coupon_events。 这非常低效,因为记录一次创建一个并导致多个查询,每个查询包括两个插入语句。 我想使用这样的单个插入查询: coupon_string = ” (‘abc’,’AAA’), (‘123′,’BBB’)” Coupon.connection.insert(“INSERT INTO coupons (code, name) VALUES”+coupon_string) 然后我需要为CouponEvent模型创建第二个插入查询,但我需要一个返回的coupon_ids列表。 是否有内置方法在插入时检索ID?

Rails – Enumerable Group_By多个关联

我想通过他们有许多关系对一组对象进行分组……就像这样 s.inventoryitems.group_by{|i| i.locations} 为了简单起见,这返回给我这样的东西: {[1, 2, 3]=>[“a”], [2]=>[“b”, “c”], []=>[“d”]} 我正在寻找这样的结果: {[1] => [“a”], [2] => [“a”,”b”,”c”], [3] => [“a”], [] => [“d”]} 我正在努力重组事情所以这一切都可以用更直观的DB和模型关联方式完成,但与此同时我需要立即实现这个并且需要用一些Ruby来解决它并且我不确定。 谢谢你的帮助!

Rails – 检查has_many关联中是否存在记录

我不确定我的问题措辞是否正确。 我有三个模型: User , Item和UserItem 。 user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item belongs_to :user user_item belongs_to :user user_item belongs_to :item 我需要一种方法来查看用户是否有一个项目来制作我的项目视图中的语句但是这里是catch,user_items具有enum status: [ :pending, approved] 。 所以我需要查看current_user是否具有某个:pending项。 例如,当用户访问item1的视图页面时,我有item_controller的show动作声明@item = Item.find_by_id(params[:id]) 。 但是,我可以用这个@item来查看用户是否有这个项目?

Rails:has_many有额外的细节吗?

虽然我不是一个完整的Ruby / Rails newb,但我仍然很绿,我正在试图弄清楚如何构建一些模型关系。 我能想到的最简单的例子是烹饪“食谱”的想法。 配方由一种或多种成分和每种成分的相关数量组成。 假设我们在所有成分的数据库中都有一个主列表。 这表明两个简单的模型: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base # recipe name, etc. end 如果我们只想将食谱与成分相关联,那就像添加适当的belongs_to和has_many 。 但是,如果我们想将其他信息与该关系联系起来呢? 每个Recipe都有一种或多种Ingredients ,但我们想要指出Ingredient的数量。 什么是Rails模型的方式? 这是一个像has_many through一样的东西吗? class Ingredient < ActiveRecord::Base # ingredient name belongs_to :recipe_ingredient end class RecipeIngredient < ActiveRecord::Base has_one :ingredient has_one :recipe # quantity end class […]

Rails根据父模型属性过滤子模型的记录

以下是1对M型号: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, … end class Upload < ActiveRecord::Base belongs_to :foto_gossip end 现在我想要Uploads.all的条件:published_at NOT NULL相应的上传的父模型?

Rails has_many通过使用source和source_type别名来处理多种类型

所以这是一个示例类 class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @ company.angels和@ company.vc_firms按预期工作。 但是,我如何拥有由两种源类型组成的@ company.investors? 这适用于投资表中投资者专栏的所有多态? 或者也许是使用范围合并所有source_type的方法? 投资模式如下: class Investment < ActiveRecord::Base belongs_to :investor, polymorphic: true belongs_to :company validates :funding_series, presence: true #, uniqueness: {scope: :company} validates :funded_year, presence: true, numericality: […]