我有两个模型,投诉和公司.投诉belongs_to以及accepts_nested_attributes公司和公司has_many投诉.
# Models
class Complaint < ActiveRecord::Base
attr_accessible :complaint, :date, :resolved
belongs_to :user, :class_name => 'User', :foreign_key => 'id'
belongs_to :company, :class_name => 'Company', :foreign_key => 'id'
has_many :replies
accepts_nested_attributes_for :company
end
class Company < ActiveRecord::Base
attr_accessible :name
has_many :complaints, :class_name => 'Complaint', :foreign_key => 'id'
has_many :branches, :class_name => 'Branch', :foreign_key => 'id'
belongs_to :industry
end
Run Code Online (Sandbox Code Playgroud)
在投诉控制器中,我尝试在新方法中构建公司.
# Complaint Controller
class ComplaintsController < ApplicationController
...
def new
@complaint = Complaint.new
@complaint.build_company
respond_to do …Run Code Online (Sandbox Code Playgroud) 我有一个: has_and_belongs_to_many :friends, :join_table => "friends_peoples".
要添加我做的朋友:@people.followers << @friend创建关系和新人员个人资料.
现在我想删除关系,而不是个人资料.
我尝试了@people.friends.delete(guilty.id)但删除了个人资料而不是关系.
任何的想法?
在rails 3中,我知道我可以使用:dependent =>:delete选项强制删除belongs_to和has_many关系上的依赖对象.不过我想知道,
如果我没有指定,默认行为是什么:dependent => ...
干杯,哈城
ruby-on-rails default-value has-many belongs-to ruby-on-rails-3
我有3个表 - 用户,事物和以下.用户可以通过下表跟踪事物,将a user_id与a 关联things_id.这意味着:
class User
has_many :things, :through => :follows
end
class Thing
has_many :users, :through => :follows
end
class Follow
belongs_to :users
belongs_to :things
end
Run Code Online (Sandbox Code Playgroud)
所以我可以毫无问题地检索thing.users.我的问题是如果在下面的表中,我有一个名为"relation"的列,所以我可以将一个关注者设置为"admin",我希望能够访问该关系.所以在循环中我可以做类似的事情:
<% things.users.each do |user| %>
<%= user.relation %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
有没有办法将关系包含在原始用户对象中?我试过了:select => "follows.relation",但它似乎没有加入该属性.
我有一个User模型,它有很多projects,Project模型可以有很多users,但也属于一个用户(即创建这个项目的用户).它必须属于一个User.它还允许用户列表与之关联,并考虑协作.
考虑到这一点,我的模型看起来像这样:
class User < ActiveRecord::Base
has_many :assigned_projects
has_many :projects, :through => :assigned_projects
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :assigned_projects
has_many :users, :through => :assigned_projects
end
class AssignedProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
Run Code Online (Sandbox Code Playgroud)
现在,当我想通过a创建一个新项目时User,我就是这样做的:
user = User.create(:name => 'injekt')
user.projects.create(:name => 'project one')
Run Code Online (Sandbox Code Playgroud)
现在,我知道这projects是通过AssignedProject连接模型提供的,这就是为什么project.user会返回nil.我挣扎,围绕让我的头是分配该项目的创建者最好的方式(其中的方式并不需要是user,它可能是creator或别的东西,描述性的,只要它的类型User).
接下来的想法是创建一个projects_created从a User …
ruby-on-rails associations has-many has-many-through belongs-to
我一直在玩STI和belongs_to/has_many关系,我有点困惑.
基于类似于以下的模型配置,我有几个问题:
class Parental < ActiveRecord::Base
end
class Mother < Parental
has_many :babies
end
class Father < Parental
has_many :babies
end
class Baby < ActiveRecord::Base
belongs_to :??????
end
Run Code Online (Sandbox Code Playgroud)
Baby属于什么?babies桌面上为外键命名/添加什么?我首先想到的是添加parental_id到babies具有类似的方法沿着Baby#owner该执行以下操作:
谢谢!
ruby-on-rails has-many single-table-inheritance sti ruby-on-rails-3
我已经设置了一个基于角色的访问控制系统,其中包含以下模型:
如果用户具有特定的UserRole,则只允许用户负责该项目.此Userrole名称为"负责项目",ID为2.
在用户模型中,有两个has_many关联:responsible_assignments和responsible_projects.仅当用户具有ID为2的UserRole"负责项目"时,此关联才有效.
是否可以在用户模型中为responsible_*关联创建条件关联,这是设置此类关系的常用方法吗?
解决这类问题的最佳做法是什么?
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
class UserRole < Role
class ProjectRole < Role
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
belongs_to :resource, :polymorphic => true
class User < ActiveRecord::Base
has_many :assignments
has_many :roles, :through => :assignments,
:class_name => "UserRole"
has_many :responsible_assignments, :class_name => "Assignment",
:conditions => { :role_id => 4 } // specific project role
has_many :responsible_projects, :through => :responsible_assignments,
:source => …Run Code Online (Sandbox Code Playgroud) conditional ruby-on-rails has-many single-table-inheritance polymorphic-associations
我是Backbone-relational的新手,我不确定使用HasMany的正确方法是什么.
我有一个Parent模型有很多children("很多"我的意思是成千上万的孩子).为了避免性能问题,我查询孩子通过自己的外键:/child/?parent=1,而不是创造了巨大的名单child_ids在Parent.但似乎这不是Backbone关系工作的方式.
所以我想知道处理这个问题的正确方法是什么.
1,更改我的json api以包含父级中的子ID列表,然后发送数千个ID作为Backbone-relational建议:
url = function(models) {
return '/child/' + ( models ? 'set/' + _.pluck( models, 'id' ).join(';') + '/' : '');
}
// this will end up with a really long url: /child/set/1;2;3;4;...;9998;9999
Run Code Online (Sandbox Code Playgroud)
2,在Backbone-relational中覆盖很多方法,让它处理这种情况.我的第一个想法是:
relations: [{
collectionOptions: function(model){
// I am not sure if I should use `this` to access my relation object
var relation = this;
return {
model: relation.relatedModel,
url: function(){
return relation.relatedModel.urlRoot + …Run Code Online (Sandbox Code Playgroud) foreign-key-relationship has-many backbone.js backbone-relational
所以这是一个示例类
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
Run Code Online (Sandbox Code Playgroud)
@ 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: true
end
Run Code Online (Sandbox Code Playgroud)
天使通过人物模型联系在一起
class Person < ActiveRecord::Base
has_many :investments, as: :investor
end
Run Code Online (Sandbox Code Playgroud)
相关金融机构模型协会:
class FinancialOrganization < ActiveRecord::Base
has_many :investments, as: :investor
has_many :companies, through: :investments
end
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails associations has-many has-many-through
这是一个例子:
假设我有一个Student对象,它与ReportCard对象有一个has_many关系.ReportCard对象有一个名为"graded"的布尔字段,标记它们已被评分.所以它看起来像:
class Student < ActiveRecord
has_many :report_cards
end
class ReportCard < ActiveRecord
# graded :boolean (comes from DB)
belongs_to :student
end
Run Code Online (Sandbox Code Playgroud)
现在,假设你要创建一个默认范围,这样如果学生没有评分的报告卡,你想看到所有这些,但如果他们至少有一个评分的ReportCard,你只想看到评分的.最后,假设您按"semester_number"命令它们.
在ReportCard上使用此范围正常工作:
scope :only_graded_if_possible, ->(student) { where(graded: true, student: student).order(:semester_number).presence || order(:semester_number) }
Run Code Online (Sandbox Code Playgroud)
但我希望它成为学生的默认范围,所以我试过:
class Student < ActiveRecord
has_many :report_cards, ->{ where(graded: true).order(:semester_number).presence || order(:semester_number) }
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用.如果整个数据库中只有一个分级的report_card,它将不会返回任何report_cards.查看运行的查询,首先它运行如下:
SELECT report_cards.* FROM report_cards WHERE reports_cards.graded = t ORDER BY semester_number ASC
Run Code Online (Sandbox Code Playgroud)
我认为这一定是礼物吗?检查存在查询的一部分,并注意它根本不过滤学生!因此,如果有一个评分的report_card,则检查通过,然后运行以下查询以获取返回的内容:
SELECT report_cards.* FROM reports_card WHERE report_card.student_id = 'student id here' AND report_card.graded = t ORDER BY semester_number …Run Code Online (Sandbox Code Playgroud) has-many ×10
associations ×3
belongs-to ×3
activerecord ×2
backbone.js ×1
conditional ×1
forms ×1
rails-models ×1
relationship ×1
ruby ×1
scope ×1
sti ×1