考虑以下模型:
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Run Code Online (Sandbox Code Playgroud)
成员资格是一个定制的多对五通过额外字段的对象.
如果我有一个人物实例,我如何访问其常规代码和django模板文件中所有成员关系的相应date_joined字段?
我们如何通过关联在has_many中设置其他参数?
谢谢.Neelesh
我有一个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
我有两个模型,开发人员和任务,
class Developer < ActiveRecord::Base
attr_accessible :address, :comment, :email, :name, :nit, :phone, :web
has_many :assignments
has_many :tasks, :through => :assignments
end
class Task < ActiveRecord::Base
attr_accessible :description, :name, :sprint_id, :developer_ids
has_many :assignments
has_many :developers, :through => :assignments
end
class Assignment < ActiveRecord::Base
attr_accessible :accomplished_time, :developer_id, :estimated_time, :status, :task_id
belongs_to :task
belongs_to :developer
end
Run Code Online (Sandbox Code Playgroud)
我通过添加一个Assignment表来处理关系,所以我可以将许多开发人员添加到一个任务中,现在我也希望能够操作我添加到连接表中的其他字段,如'estimated_time',' completed_time'...等......我在Simple_form上得到的是`
<%= simple_form_for [@sprint,@task], :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.association :developers, :as …Run Code Online (Sandbox Code Playgroud) ruby-on-rails associations has-many-through ruby-on-rails-3 simple-form
我现在感到困惑,我不知道如何删除/销毁连接表中的记录:
class Task < ActiveRecord::Base
belongs_to :schema
belongs_to :to_do
end
class Todo < ActiveRecord::Base
belongs_to :schema
has_many :tasks
end
class Shcema < AcitveRecord::Base
has_many :todos
has_many :tasks, :through => :todos
end
Run Code Online (Sandbox Code Playgroud)
>> sc = Schema.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
>> sc.tasks << Task.new
...
>> sc.tasks.delete(Task.first) # I just want to delete/destroy the join item here.
# But that deleted/destroyed the Task.first.
Run Code Online (Sandbox Code Playgroud)
如果我只想破坏关系项,我该怎么办?
所以这是一个示例类
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
我有以下三种模型(大规模简化):
class A < ActiveRecord::Base
has_many :bs
has_many :cs, :through => :bs
end
class B < ActiveRecord::Base
belongs_to :a
has_many :cs
end
class C < ActiveRecord::Base
belongs_to :b
end
Run Code Online (Sandbox Code Playgroud)
似乎A.cs在第一次使用时被缓存(每个对象),而我真的不喜欢它.
这是一个突出问题的控制台会话(已经删除了绒毛)
rails console
001 > b = B.create
002 > c = C.new
003 > c.b = b
004 > c.save
005 > a = A.create
006 > a.bs << b
007 > a.cs
=> [#<C id: 1, b_id: 1>]
Run Code Online (Sandbox Code Playgroud)
这确实如你所料.a.cs很好地通过a.bs关系.
008 > a2 = A.create
009 > …Run Code Online (Sandbox Code Playgroud) 我使用rails 4.0.3并尝试在Active-Admin中设置多个复选框.复选框选项未保存.这就是我所拥有的
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
accepts_nested_attributes_for :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
ActiveAdmin.register Product do
permit_params :title, :price, category_ids:[:id]
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Product" do
f.input :title
f.input :price
f.input :categories, :as => :check_boxes
end
f.actions
end
end
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用has_and_belongs_to_many,但仍然无法获得保存的选择.
任何指导都将受到高度赞赏.
干杯
我有以下示例模型结构:
class Category < ActiveRecord::Base
has_many :posts
scope :active, -> { where(active: true) }
end
class User < ActiveRecord::Base
has_many :posts
has_many :visible_posts, -> { joins(:category).merge(Category.active) }, class: Post
has_many :visible_posts_comments, through: :visible_posts, source: :comments
has_many :comments
end
class Post < ActiveRecord::Base
belongs_to :category
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
现在User.first.visible_posts_comments引发以下错误:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "categories"
LINE 1: ..." = "posts"."id" WHERE "posts"."user_id" = $1 AND …Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关has_many的文档和大量的教程:通过Rails中的关系,但我不能为我的生活得到它的支持.
我正在尝试将一个组添加到我的current_user(设计)中,我之间有一个表Group和User一个状态(用户的状态可以更改为该组).
每当我创建一个新组时,我都会收到错误消息 uninitialized constant Group::GroupUser
这是我的模特:
groupuser.rb
class GroupUser < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
group.rb
class Group < ActiveRecord::Base
has_many :clients
has_and_belongs_to_many :pictograms
has_many :group_users
has_many :users, :through => :group_users
accepts_nested_attributes_for :clients
validates_length_of :name, :minimum => 5
validates_presence_of :name
validates_presence_of :background
validates_presence_of :clocktype
end
Run Code Online (Sandbox Code Playgroud)
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of …Run Code Online (Sandbox Code Playgroud) has-many-through ×10
associations ×3
has-many ×2
activeadmin ×1
activerecord ×1
belongs-to ×1
caching ×1
checkbox ×1
destroy ×1
django ×1
join ×1
many-to-many ×1
simple-form ×1