标签: has-many-through

如何通过连接表填充has_many中的字段

我有一个关于活动记录关联的问题,请参考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)

activerecord ruby-on-rails has-many-through

8
推荐指数
1
解决办法
3649
查看次数

has_many通过多个数据库连接

如何通过多个数据库连接来创建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

8
推荐指数
1
解决办法
2387
查看次数

Rails has_many:through - >我应该保留主键列吗?

我有'作者'和'预订'表,加入了has_many:通过表'author_book'

据我所知,'author_book'表中的id主键字段没有任何意义......但在我提出这个想法之前,我只是想确认一下.那么,有没有理由将'id'列保留在has_many:through表中?

提前致谢...

ruby database-design ruby-on-rails primary-key has-many-through

8
推荐指数
1
解决办法
1240
查看次数

Activeadmin formtastic动态选择

我想通过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)

ruby-on-rails has-many-through formtastic activeadmin

8
推荐指数
1
解决办法
6750
查看次数

为什么此多态关联的source_type始终为0?

由于某种原因,多态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

8
推荐指数
2
解决办法
1206
查看次数

Rails/ActiveRecord has_many through:未保存对象的关联

让我们使用这些类:

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)

activerecord ruby-on-rails has-many-through

8
推荐指数
1
解决办法
2127
查看次数

在Rails has_many中保存关联记录的顺序:通过关联

我正在开发一个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,replacereplace_records,但我对最好的下一步将会失败.有没有办法扩展或安全地覆盖这些方法之一,以允许我正在寻找的功能(最好只针对特定的关联),或者是否有一个不同的,更好的方法呢?

activerecord ruby-on-rails associations has-many-through ruby-on-rails-plugins

7
推荐指数
1
解决办法
3964
查看次数

活动管理员has_many通过删除关联

我目前正在制作这样的协会:

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)

has-many-through ruby-on-rails-3 activeadmin

7
推荐指数
1
解决办法
3872
查看次数

Rails has_many:通过PG ::错误:错误:列引用"id"是模糊错误

我是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)

postgresql ruby-on-rails has-many-through

7
推荐指数
1
解决办法
5620
查看次数

Rails 5 - 如何在编辑表单中动态添加嵌套字段?

已经有一个月试图一眼就解决问题并不是很复杂:有3个模型 - 团队,用户和team_user(has_namy:through)以编辑和新团队的形式,能够动态添加该团队的成员.

场景:

  • 用户加入团队新表单
  • 表示团队的名称
  • 在名称字段之后选择用户(团队成员)
  • 单击"添加成员"按钮
  • 验证成员后,在文本字段+删除按钮对面的团队名称字段后添加
  • 从选择器中删除他的(成员)名称(因为它已经是团队成员)
  • 在selite中选择下一个用户,然后单击"添加成员"按钮
  • 按"提交"按钮以保存新团队和团队成员

难点:

  • 我试图通过gem Cocoon,但是不可能做出不同的parshaly来选择要添加到它的用户(SELECT)并添加了成员​​(全名 - 文本)
  • 如果通过<%= from_for ... remote:true%>以及控制器teams_controller中的单独控制器或新操作完成,它将是两种形式的嵌套(形状和表单团队team_user)及其第二个提交按钮.据我所知,附件表格不是gud.
  • 表单中的更改(更改团队名称和添加/删除团队成员只有在单击保存后基本上提交表单团队才有计数)

在此输入图像描述

应用程序/模型/ 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)

应用程序/控制器/ …

ajax jquery nested ruby-on-rails has-many-through

7
推荐指数
1
解决办法
2379
查看次数