Tag: 关系

ActiveRecord :: Relation#bind的目的是什么?

出于好奇 – 我正在阅读Relation :: QueryMethods模块的文档,并发现该方法: def bind(value) relation = clone relation.bind_values += [value] relation end 有谁知道这是什么? 我试图自己找,但失败了。 UPDATE 我将@bind_values使用情况跟踪到ActiveRecord::ConnectionAdapters的无底深度 – 值继续传递,直到执行低级SQL语句。 似乎各个适配器可能会使用这些。 我的猜测是它与预处理语句有关,比如SELECT * FROM ‘table’ WHERE ‘field’ = ? ,但我被困在这里。 任何人?

Rails:我如何建模偏好? 我应该使用has_many:through吗?

我有两个模型:User和HairColor。 用户只有一种头发颜色,但可以有许多头发颜色首选项。 对此进行建模的最佳方法是什么? 这是我开始做的事情: #models/user.rb class User :preferences end #models/hair_color.rb class HairColor < ActiveRecord::Base has_many :users end #models/preference.rb class Preference < ActiveRecord::Base belongs_to :user belongs_to :hair_color end 使用has_many:通过正确的方法? 如果我想将其扩展到其他属性,如“眼睛颜色”,该怎么办? (用户有一种眼睛颜色,但可能更喜欢许多眼睛颜色“

与Rails / ActiveRecord的多态habtm关系

我将如何创建与Rails / ActiveRecord的多态has_and_belongs_to_many关系? 我看到的大多数示例都涉及创建belongs_to关系,该关系将我的多态性限制为仅与一个父级相关: 表:任务 表:Tasks_Targets 表:CustomerStore 表:SoftwareSystem 在这种情况下,CustomerStore和SoftwareSystem都将属于“Targetable”类型。 根据我的理解,如果我实现大多数示例所示的多态关系,我只能将Targetable与任务关联一次 。 一些澄清可能有所帮助,因为大多数在线搜索仍然留下这种关系背后的一些理论无法解释…… 谢谢!

仅删除有多个关系

我有一个: has_and_belongs_to_many :friends, :join_table => “friends_peoples”. 要添加一个朋友,我会: @people.followers << @friend创建关系和新的个人资料。 现在我想删除关系,而不是个人资料。 我试过@people.friends.delete(guilty.id)但它删除了个人资料而不是关系。 任何的想法?

Rails accepted_nested_attributes_for child在validation时没有父集

我在validation时试图在我的子模型中访问我的父模型。 我在has_one上发现了一些关于反属性的东西,但是我的Rails 2.3.5无法识别它,所以它一定没有进入发行版。 我不确定它是否正是我所需要的。 我想根据父属性有条件地validation孩子。 我的父模型已经创建。 如果在我对父项进行update_attributes时尚未创建子项,则它无权访问父项。 我想知道如何访问这个父母。 它应该很容易,像parent.build_child这样设置子模型的parent_id,为什么在为accepts_nested_attributes_for构建子项时没有这样做? 例如: class Parent < AR has_one :child accepts_nested_attributes_for :child end class Child :some_method def some_method return self.parent.some_condition # => undefined method `some_condition’ for nil:NilClass end end 我的表格是标准的: 使用更新方法 def update @parent = Parent.find(params[:id]) @parent.update_attributes(params[:parent]) # => this is where my child validations take place end

将记录添加到has_and_belongs_to_many关系中

我有两个模型,用户和促销活动。 这个想法是促销可以有很多用户,用户可以有很多促销。 class User < ActiveRecord::Base has_and_belongs_to_many :promotions end class Promotion < ActiveRecord::Base has_and_belongs_to_many :users end 我还有一个promotion_users表/模型,没有自己的id。 它引用了user_id和promotions_id class PromotionsUsers < ActiveRecord::Base end 那么,如何将用户添加到促销中? 我尝试过这样的事情: user = User.find(params[:id]) promotion = Promotion.find(params[:promo_id]) promo = user.promotions.new(promo) 这会导致以下错误: NoMethodError: undefined method `stringify_keys!’ for # 如果我改为尝试这一行:promo = user.promotions.new(promo.id) 我收到此错误: TypeError: can’t dup Fixnum 我确信我的问题有一个非常简单的解决方案,而我只是没有以正确的方式寻找解决方案。

什么inverse_of在mongoid中意味着什么?

inverse_of在mongoid关联中的含义是什么? 我可以通过使用它而不仅仅是没有它的关联来获得什么?

在postgresql中,“数据库”和“关系”有什么区别? (’错误关系x不存在’,’错误数据库x已存在’)

我看到这两个错误的并置,并且鉴于Google搜索结果的缺乏,我不得不问。 有什么区别,我需要在这做什么? deploy=# GRANT SELECT ON angel_research_production TO angel_research; ERROR: relation “angel_research_production” does not exist deploy=# create database angel_research_production; ERROR: database “angel_research_production” already exists 我的猜测是我需要从其他一些用户那里做这个授权选择业务… 所以我在postgres(dbroot)上运行它并得到这个: postgres=# GRANT SELECT ON angel_research_production TO angel_research; ERROR: relation “angel_research_production” does not exist 因此它确实作为数据库存在,但不作为关系存在。 我怎么能纠正这个问题,这里有什么根本问题? 我有点不知所措。 谢谢

两个关系的交叉点

假设我有两个关系在同一个模型中保存记录,例如: @companies1 = Company.where(…) @companies2 = Company.where(…) 我怎样才能找到这两种关系的交集,即只有两者之间存在的公司?

Ruby on Rails:表关系搜索

我仍然无法搜索存储在单独表中的技术,其中技术表(technol)和Project表之间通过名为projecttechnol的表之间存在关系。 当我尝试使用技术(tech1)搜索项目时,这是我的日志。 Parameters: {“utf8″=>”✓”, “client”=>””, “industry”=>””, “role”=>””, “technols”=>{“id”=>[“”, “1”, “”]}, “business_div”=>””, “project_owner”=>””, “start_date_dd”=>””, “start_date_A”=>””, “start_date_B”=>””, “status”=>””, “keywords”=>””, “per_page”=>”10”} User Load (0.6ms) SELECT “users”.* FROM “users” WHERE “users”.”id” = 1 LIMIT 1 Technol Load (0.3ms) SELECT “technols”.* FROM “technols” Project Load (0.5ms) SELECT “projects”.* FROM “projects” ORDER BY client Project Load (0.6ms) SELECT “projects”.* FROM “projects” […]