Tag: 关联

Rails从两个表连接:如何选择属性

在Rails 3.2.1中 我有: class Project < ActiveRecord::Base attr_accessible :name, :description has_many :subprojects end class SubProject < ActiveRecord::Base attr_accessible :id_name, :description, :num_alloc, :project_id belongs_to :projects end 如何在rails控制器中返回,该id_name包含“name”属性(来自Project模型)和id_name , description和num_alloc (来自SubProject模型)。 在控制器中,如果我做 @results= SubProject.joins(‘LEFT OUTER JOIN…….) @results只包含SubProject类的属性,因为SubProject.joins(…)返回一个SubProject对象吗? 那么如何从两个模型中返回一个具有属性的对象呢?

Rails使用别名查询连接关联表

我有一个模型Edge通过不同的外键两次属于另一个模型Node : def Edge < ActiveRecord::Base belongs_to :first, class_name: 'Node' belongs_to :second, class_name: 'Node' end 我想使用ActiveRecord执行此查询: SELECT * FROM edges INNER JOIN nodes as first ON first.id = edges.first_id WHERE first.value = 5 我找到了使用.joins()方法加入关联的方法: Edge.joins(:first) 但这会产生使用表名而不是关联名的查询,所以在.where()方法中我必须显式地使用表名来破坏关联抽象。 Edge.joins(:first).where(nodes: {value: 5}) 我还可以在.joins()方法中明确使用SQL查询来定义模型别名: Edge.joins(‘INNER JOIN nodes as first ON nodes.id = edges.first_id’) 但这打破了更多的抽象。 我认为应该有一种方法来自动定义连接上的表别名。 或者也许是一种自己编写这种function的方法。 就像是: def Edge […]

Rails找到所有关联ID匹配的记录

我在用户和聊天之间有一个HABTM关联。 我正在尝试在其中找到包含2个特定用户的聊天。 我试过了: scope :of_users, ->(user1,user2) { joins(:users).where( users: { id: [ user1.id, user2.id ] } ) } Chat.of_users( user1, user2 ) 但是它给了我所有Chats,其中有两个用户中的一个。 我也尝试过: Chat.joins(:users) & User.where(id:1) & User.where(id:2) 没有给我正确的结果。 我在找什么? 我一直在寻找各地,但我不知道这个概念的名称…… 编辑: 我的聊天模型: class Chat (user1,user2) { joins(:users).where( users: { id: [ user1.id, user2.id ] } ) } end 我的用户型号: class User < ActiveRecord::Base […]

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:通过正确的方法? 如果我想将其扩展到其他属性,如“眼睛颜色”,该怎么办? (用户有一种眼睛颜色,但可能更喜欢许多眼睛颜色“

validation多态父级的存在

我正在使用以下模型开发Rails 3.2应用程序: class User < ActiveRecord::Base # Associations belongs_to :authenticatable, polymorphic: true # Validations validates :authenticatable, presence: true # this is the critical line end class Physician < ActiveRecord::Base attr_accessible :user_attributes # Associations has_one :user, as: :authenticatable accepts_nested_attributes_for :user end 我要做的是validation用户是否始终拥有可validation的父级。 这本身工作正常,但在我的forms中,用户模型抱怨不存在authenticatable。 我正在使用以下控制器为新医生显示一个表单,该表单接受用户的嵌套属性: def new @physician = Physician.new @physician.build_user respond_to do |format| format.html # new.html.erb […]

Rails嵌套属性:至少需要两条记录

如何才能使提交产品至少需要两个选项记录? class Product :destroy accepts_nested_attributes_for :options, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } validates_presence_of :user_id, :created_at validates :description, :presence => true, :length => {:minimum => 0, :maximum => 500} end class Option {:minimum => 0, :maximum => 60} end

模型“belongs_to”可以是/或多个模型吗?

抱歉,如果这是一个轻微的菜鸟问题,但希望澄清我对此的看法。 我有一个可以属于一个模型或另一个模型的模型。 例如: 假设我有一个团队模型,我有一个成员模型,这两个模型都可以有一个BankAccount。 class Team has_many :members has_one :bank_account end class Member belongs_to :team has_one :bank_account end class BankAccount belongs_to :team, :member end 对我而言,上述内容是有道理的,但我想与一些更有经验的Rails人员澄清这一点? Rails有没有办法计算任何给定BankAccount的父模型是什么,请记住它可能是两个模型之一? 例如,如果我在团队银行帐户上调用@ bank_account.member,它会不稳定地抛出? 谢谢你的帮助。

rails中的多列主键

我正在尝试将桌面应用程序迁移到rails(也处理相当老式的现有数据库)。 问题是我在一列中没有唯一的ID,但它是表的三列,保证了记录的唯一性。 鉴于我有三个表: authors author_name, author_letter, author_nr1, author_nr2 … titles titel_nr, titel_name, … author_titles titel_nr, author_letter, author_nr1, author_nr2 作者的“主键”包括author_letter,author_nr1,author_nr2 here。 那么我需要一种多列主键来使rails关联工作吗? 或者我在这方向走错了方向?

在validation外键时,Accept_nested_attributes_for出现问题

我正在使用Ruby on Rails v3.2.2。 我想在使用accepts_nested_attributes_for和validates_associated RoR方法时解决与validation外键有关的问题。 也就是说,我有以下模型类: class Article ‘category_id’ accepts_nested_attributes_for :category_associations, :reject_if => lambda { |attributes| attributes[:category_id].blank? } validates_associated :category_associations end class CategoryAssociation ‘article_id’ belongs_to :category, :foreign_key => ‘category_id’ validates :article_id, :presence => true validates :category_id, :presence => true end ……我有以下控制器动作: class ArticlesController < ApplicationController def new @article = Article.new 5.times { @article.category_associations.build } […]

与has_many的Simple_Form关联:通过额外字段

我有两个模型,开发人员和任务, class Developer :assignments end class Task :assignments end class Assignment < ActiveRecord::Base attr_accessible :accomplished_time, :developer_id, :estimated_time, :status, :task_id belongs_to :task belongs_to :developer end 我通过添加一个Assignment表来处理关系,所以我可以将许多开发人员添加到一个任务中,现在我也希望能够操作我添加到连接表中的其他字段,如’estimated_time’,’ completed_time’…等……我在Simple_form上得到的是` { :class => ‘form-horizontal’ } do |f| %> :check_boxes %> ‘btn-primary’ %> t(“helpers.links.cancel”)), project_sprint_path(@sprint.project_id,@sprint), :class => ‘btn’ %> ` 这只允许我选择开发人员,我希望能够在那里修改estimated_time字段。 有什么建议?