我有两个相关的模型如下。
USERS
has_many :celebrations
has_many :boards, :through => :celebrations
BOARDS
has_many :celebrations
has_many :users, :through => :celebrations
CELEBRATIONS
:belongs_to :user
:belongs_to :board
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我想从表单数据创建对象。我这样做如下:
@user = User.new(params[:user])
@board = Board.new(params[:board])
if @user.save & @board.save
@user.celebrations.create(:board_id => @board,:role => "MANAGER")
redirect_to :action => some_action
end
Run Code Online (Sandbox Code Playgroud)
由于模型是由多个模型连接起来的,有没有一种方法可以一次性保存它们,然后一次生成错误消息,以便它们同时显示在表单上?
我有一个 Rails 6 应用程序 (6.0.0rc1)。它的书籍和作者模型具有 has_many :through 关联。我创建了一个连接表
rails g migration CreateJoinTableAuthorsBook author:references books:references
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我错误地命名了该表,并且错误地引用了该书。我删除了迁移并创建了新的迁移
rails g migration CreateJoinTableAuthorsBooks author:references book:references
Run Code Online (Sandbox Code Playgroud)
现在,当我运行迁移时,我得到了
ArgumentError: you can't define an already defined column 'author_id'.
Run Code Online (Sandbox Code Playgroud)
我已经搜索了整个 Rails 应用程序,但在任何地方都找不到“author_id”。我运行了上一次迁移(值不正确的迁移)。如何删除之前的“author_id”以便成功运行迁移?
我有三个模型(这里简化):
class Child < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :observations, :through => :childviews
end
class Childview < ActiveRecord::Base
belongs_to :observation
belongs_to :child
end
class Observation < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :children, :through => :childviews
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails的to_json方法将此发送给一些JavaScript,如下所示:
render :layout => false , :json => @child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
Run Code Online (Sandbox Code Playgroud)
这非常有效.通过连接表(子视图)可以很好地检索观察结果.
但是,我还想获取位于childviews连接表中的数据; 特别是'needs_edit'的值.
我无法弄清楚如何在to_json调用中获取此数据.
谁能帮我?提前谢谢了.
qryss
我正在我的 rails 应用程序中创建行程,我想为这些行程添加类别。类别存储在我的数据库的类别表中,用户可以选择适合旅行的类别。所以一个行程可以使用多个类别。
虽然我是一个菜鸟,但我在有关这个主题的 RoR 指南的帮助下想出了一些事情。现在,我有一个第三个表tripcategories,它必须保存trip_id 和category_id。对?有了它,我有以下模型:
旅行.rb:
class Trip < ActiveRecord::Base
attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo
has_many :triplocations, :dependent => :destroy
has_many :tripcategories
has_many :categories, :through => :tripcategories
accepts_nested_attributes_for :triplocations, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
类别.rb:
class Category < ActiveRecord::Base
attr_accessible :name
has_many :tripcategories
belongs_to :trip, :through => :tripcategories
end
Run Code Online (Sandbox Code Playgroud)
tripcategory.rb:
class Tripcategory < ActiveRecord::Base
attr_accessible :category_id, :trip_id
belongs_to :trip
belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)
当我以这种方式尝试并尝试在我的旅行索引中调用 trip.categories 时,它说"Unknown key: through"。我做错了什么,还是我错过了大局?
提前致谢!
我有两个模型,我使用多态的has_many通过关联链接在一起,我想添加一个counter_cache但似乎Rails/ActiveRecord不支持开箱即用的这个功能.
class Classifiable < ActiveRecord::Base
has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id
end
class Taxonomy < ActiveRecord::Base
has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id
end
class Question < Classifiable
has_many :categories, :through => :classifications, :as => :classifiable, :source => :taxonomy, :source_type => "Category"
end
class Category < Taxonomy
has_many :questions, :through => :classifications, :source => :classifiable, :source_type => "Question"
end
class Classification < ActiveRecord::Base
attr_accessible :classifiable, :classifiable_id, :classifiable_type,
:taxonomy, :taxonomy_id, :taxonomy_type
belongs_to :classifiable, :polymorphic => true
belongs_to :taxonomy, …Run Code Online (Sandbox Code Playgroud) polymorphism activerecord ruby-on-rails has-many-through counter-cache
通过关系,我有一个标准的has_many.人类通过连接表交互有许多兽人.互动只是一个表格和模型; 没有控制器或视图.
使用Rails 4中的simpleform gem,我想从人体页面创建一个表单,以便从所有兽人的池中选择多个兽人.提交后,我希望它在交互表中创建/更新尽可能多的记录,每个记录都包含人工ID,并且选择了多个orc ID.:
AKA列表符号
human_id并且orc_id从该列表中选择兽人.(human_id在这些记录中将是相同的,因为它从给定的人类表单页面开始)我将尽可能多地编写整个故事的代码.请随时要求澄清,并解决任何错误,以实现这一点.
表
humans
integer "id"
interactions
integer "human_id"
integer "orc_id"
index ["human_id", "orc_id"]
# This is the primary key. no normal id.
# Is it better to have a primary id for this join table, or does it not matter?
orcs
integer "id"
Run Code Online (Sandbox Code Playgroud)
楷模
/models/human.rb
class Human < ActiveRecord::Base
has_many :interaction
has_many :orcs, through: :interactions
accepts_nested_attributes_for :interactions
end
Run Code Online (Sandbox Code Playgroud)
/models/interaction.rb
# Purely a join model and …Run Code Online (Sandbox Code Playgroud) 我有以下型号
class User < ActiveRecord::Base
has_many :user_wishes
has_many :wishes, through: :user_wishes
end
class UserWish < ActiveRecord::Base
belongs_to :user
belongs_to :wish
end
class Wish < ActiveRecord::Base
has_many :user_wishes
has_many :users, through: :user_wishes
end
Run Code Online (Sandbox Code Playgroud)
现在我有以下界面,用户可以在其中选择/取消选择愿望.

一旦用户完成选择/取消选择,他就提交表单.现在我在后端做的就是user_wishes为给定的user_id&创建或销毁记录wish_id.
现在有两种方法可以实现我能想到的解决方案.
假设用户已经有N个愿望,现在他提出了M愿望的新请求.我将删除(N - M)的差异并添加(M - N)的差异.这种方法似乎非常低效,因为我总是必须通过网络发送(N∩M).
端点: /user_wishes
TYPE : POST ? 但它也删除了记录
参数: {wish_id: [1,2 ..], user_id: 10}
我将在前端有逻辑,它给出了一个新添加/删除的愿望列表.
端点: /user_wishes
TYPE : POST ? 但它也删除了记录
参数: …
我有一个包含事务的表,其中每个事务都Transaction属于 aDriver或 a Customer- 所以我在它们之间设置了多态关系。
对于交易我已经设置:
public function owner() {
return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)
对于司机和客户:
public function transactions() {
return $this->morphMany(Transaction::class, 'owner');
}
Run Code Online (Sandbox Code Playgroud)
但每个驱动程序也属于一个Company. 我正在尝试获取属于Company直通hasManyThrough关系的所有交易:
public function transactions() {
return $this->hasManyThrough(Transaction::class, Driver::class);
}
Run Code Online (Sandbox Code Playgroud)
但它似乎不适用于多态关系,因为它会抛出错误,因为它试图driver_id在transactions表中查找字段。
通过驱动程序获取属于公司的所有交易的方法是什么?
relationship has-many-through polymorphic-associations laravel eloquent
假设我有一个名为Device的模型,另一个名为Firmware的模型(包含固件版本号和其他信息).我想跟踪设备上安装的固件版本的历史记录,还要参考当前版本(可能是也可能不是最新的日期版本).
如何设置关联以便指向一个特定的固件配置(当前固件配置)以及几个不同的固件配置(历史记录)?
我已经看到了几个同样问题的问题,例如
通过Active Admin使用HABTM或Has_many
但我仍然在努力让事情发挥作用(此时我已尝试过多种方式).
我的模型(稍微复杂于用户模型的'技术员'别名):
class AnalysisType < ActiveRecord::Base
has_many :analysis_type_technicians, :dependent => :destroy
has_many :technicians, :class_name => 'User', :through => :analysis_type_technicians
accepts_nested_attributes_for :analysis_type_technicians, allow_destroy: true
end
class User < ActiveRecord::Base
has_many :analysis_type_technicians, :foreign_key => 'technician_id', :dependent => :destroy
has_many :analysis_types, :through => :analysis_type_technicians
end
class AnalysisTypeTechnician < ActiveRecord::Base
belongs_to :analysis_type, :class_name => 'AnalysisType', :foreign_key => 'analysis_type_id'
belongs_to :technician, :class_name => 'User', :foreign_key => 'technician_id'
end
Run Code Online (Sandbox Code Playgroud)
我已经为AnalysisType模型注册了一个ActiveAdmin模型,并且希望能够选择(已经创建)技术人员以在下拉列表/复选框中与该AnalysisType相关联.我的ActiveAdmin设置目前看起来像:
ActiveAdmin.register AnalysisType do
form do |f|
f.input :analysis_type_technicians, as: :check_boxes, :collection => User.all.map{ …Run Code Online (Sandbox Code Playgroud) has-many-through ×10
associations ×3
activeadmin ×1
activerecord ×1
eloquent ×1
json ×1
laravel ×1
nested-forms ×1
polymorphism ×1
relationship ×1
rest ×1
save ×1
simple-form ×1