簡體   English   中英

關系在Ruby on Rails上的belongs_to與belongs_to之間

[英]relationship belongs_to to belongs_to in ruby on rails

我在Rails的兩個模型之間有一個歸屬關系到歸屬關系。 我有模型用戶和模型啟動

用戶模型是一個設計模型(gem devise)。

這是我的代碼。

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
  # attr_accessible :title, :body

  belongs_to :startup
end

這是我的模型啟動

class Startup < ActiveRecord::Base
  attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
  has_one :entrepreneus, dependent: :destroy

  belongs_to :user
end

同樣在我的架構中,我在啟動表中添加了“ user_id”。

這是添加的add_index

  add_index "startups", ["user_id"], :name => "index_startups_on_user_id"

(顯然我進行了遷移)

當我在第一時間創建啟動時,創建不帶user_id的對象,但帶update_attributes的對象將user_id添加到對象中。 (在startup_controller中)。 這是代碼

def create
    @startup = Startup.new(params[:startup])
    @startup.update_attributes(:user_id => current_user.id )

    respond_to do |format|
      if @startup.save
        format.html { redirect_to @startup, notice: 'Startup was successfully created.' }
        format.json { render json: @startup, status: :created, location: @startup }
      else
        format.html { render action: "new" }
        format.json { render json: @startup.errors, status: :unprocessable_entity }
      end
    end
  end

在控制台中,我執行以下操作(rails c --sandbox)

u =  User.first (retrieve the user perfectly, the id is equal to 1)

u.startup (retrieve null)

但是,如果您這樣做:

s = Startup.find_by_user_id(1) // retrieve the data

為什么我無法通過u.startup檢索與啟動相關的數據?

任何想法 ?。 謝謝

PDT:對不起,我的英語還不是很好。

如果這是兩個模型之間的一對一關系,則必須在表中沒有外鍵的一側使用has_one 因此,在這種情況下,請使用:

belongs_to :startup

你必須做:

has_one :startup

我是Rails的新手,所以如果我錯了,請有人糾正我,但我認為您不能擁有2個彼此“屬於”的模型。 1必須屬於,1必須具有has_many或has_one(編輯:根據注釋,應該為:has_one

我認為這可能是您要尋找的:

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
  # attr_accessible :title, :body

  has_one :startup # not has_many :startups
end

class Startup < ActiveRecord::Base
  attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
  has_one :entrepreneus, dependent: :destroy

  belongs_to :user
end

暫無
暫無

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

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