我有一个关于活动记录关联的问题,请参考rails文档的这一部分:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
如果我们有三个型号:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
Run Code Online (Sandbox Code Playgroud)
文档说可以通过api以这种方式管理连接模型的集合:
physician.patients = patients
Run Code Online (Sandbox Code Playgroud)
但是,如果约会模型(如链接示例中)有一个名为appointment_date的字段,并且我想在特定日期为医生和患者创建新约会,该怎么办?以下代码将在约会表中创建一条记录,但是如何在第三步中填充appointment_date呢?
physician = Physician.first
patient = Patients.first
physician.patients << patient
Run Code Online (Sandbox Code Playgroud)
这样的事情存在吗?
physician.patients.create( :patient => patient, 'appointment.appointment_time' => appointment_time )
Run Code Online (Sandbox Code Playgroud) 如何通过多个数据库连接来创建has_many?
我有一个名为"master"的数据库,用于保存位置信息.这是从单独的应用程序更新.用户可以访问许多位置,但所有其他模型都位于另一个名为"预算"的数据库中.以下是模型的设置方法.
# place.rb
class Place < ActiveRecord::Base
belongs_to :user
belongs_to :location
end
# user.rb
class User < ActiveRecord::Base
has_many :locations, :through => :places
has_many :places
end
# location.rb
class Location < ActiveRecord::Base
establish_connection "master"
has_many :places
has_many :users, :through => :places
end
Run Code Online (Sandbox Code Playgroud)
当我通过irb运行命令时,我得到以下内容
> Location.first.places.create(:user_id => 1)
> #<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43", updated_at: "2011-11-28 20:58:43">
> Location.first.places
> [#<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43", updated_at: "2011-11-28 20:58:43">]
> Location.first.users …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails has-many-through ruby-on-rails-3.1
我有'作者'和'预订'表,加入了has_many:通过表'author_book'
据我所知,'author_book'表中的id主键字段没有任何意义......但在我提出这个想法之前,我只是想确认一下.那么,有没有理由将'id'列保留在has_many:through表中?
提前致谢...
ruby database-design ruby-on-rails primary-key has-many-through
我想通过Activeadmin的formtastic制作动态选择选项,如下所示:
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
#selects user from list. WORKING
f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user)
#collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons.
f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user)
#collection of lessons will change to reflect lessons connected by chosen user and student. NOT …Run Code Online (Sandbox Code Playgroud) 由于某种原因,多态has_many :through关联的源类型始终为0,尽管设置了a :source_type.
这就是我的模特的样子......
富:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
Run Code Online (Sandbox Code Playgroud)
酒吧:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
Run Code Online (Sandbox Code Playgroud)
TaggedItem:
belongs_to :tag
belongs_to :taggable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
标签:
has_many :tagged_items
has_many :foos, :through => :tagged_items, :source => :taggable, :source_type => "Foo"
has_many :bars, :through => :tagged_items, :source => :taggable, :source_type => "Bar"
Run Code Online (Sandbox Code Playgroud)
尽可能接近我可以说这是一个非常精细的设置,我能够创建/添加标签,但taggable_type总是最终为0.
任何想法都是为什么?谷歌一无所获.
ruby-on-rails has-many-through polymorphic-associations ruby-on-rails-4
让我们使用这些类:
class User < ActiveRecord::Base
has_many :project_participations
has_many :projects, through: :project_participations, inverse_of: :users
end
class ProjectParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :project
enum role: { member: 0, manager: 1 }
end
class Project < ActiveRecord::Base
has_many :project_participations
has_many :users, through: :project_participations, inverse_of: :projects
end
Run Code Online (Sandbox Code Playgroud)
A user可以参与许多projects角色扮演a member或a manager.调用连接模型ProjectParticipation.
我现在在使用未保存对象上的关联时遇到问题.以下命令的工作方式与我认为应该有效相同:
# first example
u = User.new
p = Project.new
u.projects << p
u.projects
=> #<ActiveRecord::Associations::CollectionProxy [#<Project id: nil>]>
u.project_participations
=> #<ActiveRecord::Associations::CollectionProxy [#<ProjectParticipation id: nil, …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Rails插件,其中包含一种修改has_many中关联记录顺序的方法:通过关联.假设我们有以下型号:
class Playlist < ActiveRecord::Base
has_many :playlists_songs, :dependent => :destroy
has_many :songs, :through => :playlists_songs
end
class Song < ActiveRecord::Base
has_many :playlists_songs, :dependent => :destroy
has_many :playlists, :through => :playlists_songs
end
class PlaylistsSong < ActiveRecord::Base
belongs_to :playlist
belongs_to :song
end
Run Code Online (Sandbox Code Playgroud)
如果我们改变播放列表歌曲的顺序(例如@playlist.songs.rotate!),Rails不会触及playlists_songs表中的记录(我正在使用Rails 3.1),这是有道理的.我想调用播放列表的歌曲=方法保存歌曲的顺序,但是,可以通过删除播放列表中的相关现有行并以正确的顺序创建新的行(这样:order => "id"可以在检索它们时使用)或者通过向playlists_songs添加sort:integer列并相应地更新这些值.
我没有看到任何允许这样做的回调(例如before_add).在ActiveRecord :: Associations :: CollectionAssociation中,相关的方法似乎是writer,replace和replace_records,但我对最好的下一步将会失败.有没有办法扩展或安全地覆盖这些方法之一,以允许我正在寻找的功能(最好只针对特定的关联),或者是否有一个不同的,更好的方法呢?
activerecord ruby-on-rails associations has-many-through ruby-on-rails-plugins
我目前正在制作这样的协会:
show do
h3 project.title
panel "Utilisateurs" do
table_for project.roles do
column "Prenom" do |role|
role.user.firstname
end
column "Nom" do |role|
role.user.lastname
end
column "email" do |role|
role.user.email
end
column "Role" do |role|
role.role_name.name
end
end
end
end
# override default form
form do |f|
f.inputs "Details" do # Project's fields
f.input :title
f.input :code
end
f.has_many :roles do |app_f|
app_f.inputs do
# if object has id we can destroy it
if app_f.object.id
app_f.input :_destroy, :as => :boolean, :label => …Run Code Online (Sandbox Code Playgroud) 我是rails的新手,我一直在试图获得两个has_many:虽然关系可以解决(而不是像本帖所解释的那样使用has_and_belongs_to_many http://blog.flatironschool.com/post/35346328762/why-you -dont-need-have-and-belongs-to-many)但现在遇到Postgres错误:
PG::Error: ERROR: column reference "id" is ambiguous
LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM...
^
: SELECT 1 AS one FROM "components" INNER JOIN "collection_components" ON "components"."id" = "collection_components"."component_id" WHERE "collection_components"."collection_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIMIT 1
Rendered collections/_form.html.haml (117.0ms)
Rendered collections/new.html.haml within layouts/application (143.5ms)
Completed 500 Internal Server Error in 164ms
ActiveRecord::StatementInvalid - PG::Error: ERROR: column reference "id" is ambiguous
LINE 1: …Run Code Online (Sandbox Code Playgroud) 已经有一个月试图一眼就解决问题并不是很复杂:有3个模型 - 团队,用户和team_user(has_namy:through)以编辑和新团队的形式,能够动态添加该团队的成员.
场景:
难点:
应用程序/模型/ user.rb
class User < ApplicationRecord
has_many :team_users
has_many :teams, through: :team_users
accepts_nested_attributes_for :team_users, :teams, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ team.rb
class Team < ApplicationRecord
has_many :team_users
has_many :users, through: :team_users
accepts_nested_attributes_for :team_users, allow_destroy: true, reject_if: proc { |a| a['user_id'].blank? }
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ team_user.rb
class TeamUser < ApplicationRecord
belongs_to :team
belongs_to :user
accepts_nested_attributes_for :team, :user, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ …
has-many-through ×10
activerecord ×4
activeadmin ×2
ajax ×1
associations ×1
formtastic ×1
jquery ×1
nested ×1
postgresql ×1
primary-key ×1
ruby ×1