簡體   English   中英

努力通過關系添加has_many

[英]Struggling to add has_many through relationship

我試圖通過這樣的關系來建立has_many:

#user.rb
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :availabilities
  has_many :timeslots, :through => availabilities
end

#availability.rb
class Availability < ApplicationRecord
    belongs_to :timeslot
    belongs_to :user
end

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :timeslots, :through => availabilities
end

我創建了兩個模型,然后運行了rake db:migrate而沒有在模型中添加代碼(創建表)。 我做了一個遷移文件:

class AddFieldsToTables < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :availability_id, :integer
    add_column :timeslots, :availability_id, :integer
    add_column :availabilities, :user_id, :integer
    add_column :availabilities, :timeslot_id, :integer
  end
end

並運行rake db:migrate將上述代碼添加到所有文件中。 然后,如果我嘗試生成任何東西,它將給我NameError: undefined local variable or method availabilities for User (call 'User.connection' to establish a connection):Class

我是Ruby on Rails的新手。

我在您的代碼中看到一個小問題:

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :timeslots, :through => availabilities
end

它應該是:

#timeslot.rb
class Timeslot < ApplicationRecord
    has_many :availabilities
    has_many :users, :through => availabilities
end

我不確定它是否可以解決您的問題,但是您的代碼(排除上述錯誤)對我來說聽起來不錯。

我看到的一個問題是,在您的timeslot.rb您具有has_many :timeslots, :through => availabilities 我猜你想要has_many :users, :through => :availabilites

另一個是在user.rb ,您具有has_many :timeslots, :through => availabilities但是需要符號:availabilites 我相信,這就是導致您發布的錯誤的原因。 它看起來應該像這樣(我更改的是倒數第二行):

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :availabilities
  has_many :timeslots, :through => :availabilities
end

為了has_many through兩個表userstimeslots之間的關系來設置has_many through ,您需要使用user_idtimeslot_id列來設置聯接表的availabilities

如下設置您的rails模型:

# models/user.rb
class User < ApplicationRecord
  has_many :availabilities
  has_many :timeslots, :through => :availabilities
end

# models/availability.rb
# projects table should have these columns - user_id:integer, timeslot_id:integer
class Availability < ApplicationRecord
  belongs_to :timeslot
  belongs_to :user
end    

# models/timeslot.rb
class Timeslot < ApplicationRecord
  has_many :availabilities
  has_many :users, :through => :availabilities
end

您需要遷移才能創建availabilities表,該表通過Timeslot對象和User對象之間的關系充當has_many的聯接表。 遷移文件如下所示:

class CreateAvailabilities < ActiveRecord::Migration[5.0]
  def change
    create_table :availabilities do |t|
      t.integer :user_id
      t.integer :timeslot_id
    end
  end
end

訪問

User.last.timeslots給出與User.last關聯的User.last或許多時隙

Timeslot.last.users提供0、1個或多個與Timeslot.last相關的用戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM