Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用另一个表中的对象为ActiveRecord数据库设定种子_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 使用另一个表中的对象为ActiveRecord数据库设定种子

Ruby on rails 使用另一个表中的对象为ActiveRecord数据库设定种子,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我是Rails新手,我正在制作我的第一个应用程序,我还有一个问题: 我正在将卡片组功能添加到我的flashcard应用程序中,但在建模以生成由我的卡片表中的卡片对象填充的卡片组时遇到了问题 以下是我的联想: User has_many :decks Deck belongs_to :user Deck has_many :cards Card belongs_to :deck class Card < ActiveRecord::Base belongs_to :deck

我是Rails新手,我正在制作我的第一个应用程序,我还有一个问题:

我正在将卡片组功能添加到我的flashcard应用程序中,但在建模以生成由我的卡片表中的卡片对象填充的卡片组时遇到了问题

以下是我的联想:

User has_many :decks

Deck belongs_to :user
Deck has_many :cards

Card belongs_to :deck




class Card < ActiveRecord::Base
  belongs_to :deck

  validates :front, presence: true
  validates :back, presence: true
  validates :deck_id, presence: true

end
这是我的桌子:

class Decks < ActiveRecord::Migration
  def change
    create_table :decks do |t|
      t.string :name, null: false
      t.string :card
      t.integer :user_id, null: false
    end
  end
end
我的问题是,我希望decks表中的card列由card对象组成,这样我就可以访问/操作它们的方法,但我不确定具体如何做到这一点。我试着用t.string of:card填充表格,希望这能起作用,但结果却是空白。我想知道这是否可行或可取,或者是否有更好的方法


如果有人能给我提供这方面的资源/建议,谢谢。我查了一下文件,似乎什么也没找到

对于has\u many关系,您不会将外键存储在所属表上。相反,在cards表中有一个deck_id列

使用关联的示例如下:

# Load a deck and include the cards in the query
@deck = Deck.joins(:cards).last

@deck.cards.each do |card|
  puts card.front
end

# create a new card
@deck.cards.new(front: 'foo', back: 'bar')
@deck.save # will save the card as well.

对于has\u many关系,您不会将外键存储在所属表上。相反,在cards表中有一个deck_id列

使用关联的示例如下:

# Load a deck and include the cards in the query
@deck = Deck.joins(:cards).last

@deck.cards.each do |card|
  puts card.front
end

# create a new card
@deck.cards.new(front: 'foo', back: 'bar')
@deck.save # will save the card as well.
你不希望卡片列出现在你的卡片组表中。在您的模型中,有许多:卡片和属于:卡片组线条为您提供了功能,使您能够执行@deck.cards之类的操作

创建新卡对象时,您只需确保分配一个卡组id

您应该通读上和上的Rails指南。

您不希望卡片列出现在您的卡片组表中。在您的模型中,有许多:卡片和属于:卡片组线条为您提供了功能,使您能够执行@deck.cards之类的操作

创建新卡对象时,您只需确保分配一个卡组id


您应该通读上和上的Rails指南。

您所描述的内容很容易完成

如果用户、组和卡是ActiveRecord模型,则可以通过设置外键来连接表来关联它们。外键是一个整数列,其中包含关联模型的id及其表的主键

Rails约定使用的是属于多个关联并有多个关联来声明一对多关联。这些方法将向模型对象添加表示关联并与关联交互所需的方法

在DB模式方面,您需要在模型的表上设置外键,这些表声明了所属的关联

因此,如果您有这些模型:

class User < ActiveRecord::Base
  has_many :decks
end

class Deck < ActiveRecord::Base
  belongs_to :user
  has_many :cards
end

class Card < ActiveRecord::Base
  belongs_to :deck
end
您将需要以下迁移:

class CreateDecks < ActiveRecord::Migration
  def change
    create_table :decks do |t|
      # your other columns...
      t.integer :user_id
    end
  end
end

class CreateCards < ActiveRecord::Migration
  def change
    create_table :cards do |t|
      # your other columns...
      t.integer :deck_id
    end
  end
end

你所描述的很容易完成

如果用户、组和卡是ActiveRecord模型,则可以通过设置外键来连接表来关联它们。外键是一个整数列,其中包含关联模型的id及其表的主键

Rails约定使用的是属于多个关联并有多个关联来声明一对多关联。这些方法将向模型对象添加表示关联并与关联交互所需的方法

在DB模式方面,您需要在模型的表上设置外键,这些表声明了所属的关联

因此,如果您有这些模型:

class User < ActiveRecord::Base
  has_many :decks
end

class Deck < ActiveRecord::Base
  belongs_to :user
  has_many :cards
end

class Card < ActiveRecord::Base
  belongs_to :deck
end
您将需要以下迁移:

class CreateDecks < ActiveRecord::Migration
  def change
    create_table :decks do |t|
      # your other columns...
      t.integer :user_id
    end
  end
end

class CreateCards < ActiveRecord::Migration
  def change
    create_table :cards do |t|
      # your other columns...
      t.integer :deck_id
    end
  end
end

谢谢大家的解释。。。。我已经按照文档为我的卡模型分配了一个deck_id,但我不明白这足以通过@deck.cards这样的表达式访问所有cards方法。我的数据库种子,但它的工作。谢谢大家的解释。。。。我已经按照文档为我的卡模型分配了一个deck_id,但我不明白这足以通过@deck.cards这样的表达式访问所有cards方法。我的数据库种子,但它的工作。谢谢