简体   繁体   English

ruby on rails association新手

[英]ruby on rails associations newbie

This might be considered as a re-post of rails 3 associations error . 这可能被视为rails 3关联错误的重新发布。 But its something which I am not able to understand . 但它是我无法理解的东西。 But ok ... heres the deal.. I have asked a question in here which was related to a table schema. 但是好的......继续这个交易......我在这里问了一个与表模式有关的问题。 The post is what is bad in this table schema structure and the query was : 帖子是这个表模式结构中的错误 ,查询是:

`CREATE TABLE "pages" (
   "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
   "title" varchar(255),
   "body" varchar(255),
   "author" varchar(255), 
   "email" varchar(255), 
   "reference" varchar(255),
   "created_at" datetime,
   "updated_at" datetime);`

I was suggested that I should create separate tables for author, and that is what I tried and messed up. 我被建议我应该为作者创建单独的表,这就是我尝试和搞砸了。 Can anyone please tell me how exactly should the associations work with my scenario? 任何人都可以告诉我协会应该如何与我的方案一起工作? I really need to understand the ROR associations well. 我真的需要很好地理解ROR协会。

edit-1 编辑-1

Here is my pages migration code: 这是我的页面迁移代码:

class CreatePages < ActiveRecord::Migration
  def self.up
    create_table :pages do |t|
      t.string :title, :unique => true, :limit => 50
      t.string :body, :limit =>255
      t.string :email, :unique => true, :limit => 50
      t.string :reference, :limit => 40
      t.timestamps
    end

  end

  def self.down
    drop_table :pages
  end
end

and here is my author migrations: 这是我的作者迁移:

class CreateAuthors < ActiveRecord::Migration
  def change
    create_table :authors do |t|
      t.string :author, :unique => true, :null => false, :limit => 50
      t.timestamps
    end
  end
end

If we think this through logically, an author might have many pages, and pages belong to an author. 如果我们通过逻辑思考,作者可能有很多页面,页面属于作者。 If that's the case, we can express this through rails relationships. 如果是这种情况,我们可以通过rails关系来表达这一点。

class Author < ActiveRecord::Base
  has_many :pages

class Page < ActiveRecord::Base
  belongs_to :author

In order to support this, you'll need to create a migration to ensure that there is an author_id column in your pages table. 为了支持这一点,您需要创建一个迁移以确保您的pages表中有author_id列。 Rails takes care of the rest. Rails负责其余部分。 You'll get all the relationship semantics like: 您将获得所有关系语义,如:

my_page.author.email
my_page.author.name

(assuming that your Author class has such attributes) (假设您的Author类具有此类属性)

Note that you don't necessarily have to have an Author class if your Users are your authors. 请注意,如果您的用户是您的作者,则不一定必须拥有Author类。 If every Author is a User (and you use your User class for authentication and account maintenance), you can still refer to the User as an Author of a class 如果每个作者都是用户(并且您使用User类进行身份验证和帐户维护),您仍然可以将用户称为类的作者

class Page < ActiveRecord::Base
  belongs_to :author, :class_name => "User"

which lets you keep the nice author semantics by does them against your User class. 这可以让你保持好作者的语义,它们对你的User类。

Final note, if you want to automatically delete any pages associated with an author when you delete an author object, you can add dependence to the Author class 最后注意,如果要在删除作者对象时自动删除与作者关联的任何页面,可以向Author类添加依赖关系

class Author < ActiveRecord::Base
  has_many :pages, :dependent => :destroy

Creating a separate table for authors is probably a good idea (although if this working well for you already, and you don't have any performance problems, you could just leave it as is). 为作者创建一个单独的表可能是一个好主意(尽管如果这对你来说效果很好,并且你没有任何性能问题,你可以保持原样)。

@Marc Talbot already gave a good answer about how to set the associations up in your Rails models. @Marc Talbot已经就如何在Rails模型中设置关联给出了一个很好的答案。 Of course, you will also need to migrate your database. 当然,您还需要迁移数据库。 This is what to do: 这是做什么的:

  1. Make a table called "authors". 制作一个名为“作者”的表格。 Besides the primary key "id", it should have a field called "name", as well as "email". 除了主键“id”之外,它还应该有一个名为“name”的字段,以及“email”。 You can move any other fields which will logically always be the same for the same author from "pages" to "authors". 您可以将同一作者在逻辑上始终相同的任何其他字段从“pages”移动到“authors”。
  2. Add an integer field "author_id" to "pages". 将整数字段“author_id”添加到“pages”。
  3. Run over the existing entries in "pages". 运行“页面”中的现有条目。 For each page, check whether the author is already in "authors". 对于每个页面,检查作者是否已经在“作者”中。 If not, add the author to "authors", and fill in "name", "email", and the other fields in "authors". 如果没有,请将作者添加到“authors”,并填写“name”,“email”以及“authors”中的其他字段。 Set "author_id" to refer to the correct ID from the authors table. 设置“author_id”以引用authors表中的正确ID。
  4. Drop "author", "email", and the other moved fields from "pages". 从“页面”中删除“作者”,“电子邮件”和其他移动的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM