Rails 模型关系 [英] Rails Models relationships

查看:58
本文介绍了Rails 模型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位开发者好!最近我一直在玩 Rails 3.0,经过大量研究后,我有点卡住了.我想知道在我的情况下哪种方法或解决方案是最好的(我还找不到答案).所以我想要实现的目标很简单直接.

Hello fellow developers! Recently I've been playing with Rails 3.0 and after quite a bit of research I'm kinda stuck. I want to know what approach or solution is the best in my case(I couldn't find an answer yet). So what I'm trying to achieve is simple and straight forward.

我想做这样的事情:

class User < ActiveRecord::Base
    has_many :feeds
    has_many :casts, :through => :feeds
end

class Feed < ActiveRecord::Base
  has_many :users
  has_many :casts
end

class Cast < ActiveRecord::Base
  belongs_to :feed
end

所以最后我需要像 User.first.feeds 这样的方法来获取所有用户的提要和 User.first.casts 来通过他/她的提要获取所有用户的演员表.拥有 Feed.first.casts 和 Feed.first.users 也会很好.很简单,对,但我也很难为我想要实现的目标创建迁移.

So at the end I need to have methods like User.first.feeds to get all the user's feeds and User.first.casts to get all the user's casts through his/her feeds. Also would be nice to have Feed.first.casts and Feed.first.users. Pretty simple, right, but I'm also having a hard time to create migrations for what I'm trying to achieve.

我知道上面的代码不起作用 - 我一直在玩它,所以这只是我想要实现的概念.

I know that the code above won't work - I've been playing with it so this is just the concept of what I'm trying to achieve.

基本上我的问题是:我应该以某种方式通过连接模型来完成还是使用范围?(你也可以提供一个代码片段)以及我如何进行迁移?

Basically my questions are: should I do it through join model somehow or use scopes?(also could you give a code snippet) and how do I do migration for that?

谢谢,很抱歉,我在网上找不到关于这个简单案例的太多信息.

Thanks, and sorry I couldn't find much information on the web regarding this simple case.

用户和Feed上的has_and_belongs_to_many在我的情况下不起作用,因为它不允许我拥有@user.casts,它只提供@user.feeds和@feed.users

has_and_belongs_to_many on User and Feed won't work in my case because it won't allow me to have @user.casts, it gives only @user.feeds and @feed.users

推荐答案

您想要的是 User 和 Feed 之间的多对多关系.

What you want is a many to many relationship between User and Feed.

您需要在您的代码中使用类似的内容,以便用户和供稿关系正常工作.

You'll want something like this in your code for the User and Feed relationship to work.

class User < ActiveRecord::Base
  has_and_belongs_to_many :feeds
end

class Feed < ActiveRecord::Base
  has_and_belongs_to_many :users
end

您可以在 Rails 指南中阅读更多相关信息 - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

You can read more about this in the Rails Guides - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

您可能还想考虑使用 has_many :through 和一个中间模型(在此处解释 http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many) 如果你想存储任何元数据用于用户供稿关系记录.

You may also want to look at using has_many :through with an intermediate model for this (explained here http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many) if you'd like to store any meta data for a user-feed relationship record.

我设法在 3.0 和 3.1 上进行了类似的设置(使用 has_many :through).

I managed to get a similar setup working on 3.0 as well as 3.1 (using has_many :through).

这是我的模型的内容.

➜  app  cat app/models/*
class Cast < ActiveRecord::Base
  belongs_to :feed
end

class Feed < ActiveRecord::Base
  has_many :subscriptions
  has_many :casts
end

class Subscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :feed
end

class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :feeds, :through => :subscriptions

  # For 3.1
  has_many :casts, :through => :feeds
  # For 3.0
  def casts
    Cast.joins(:feed => :subscriptions).where("subscriptions.user_id" => self.id)
  end
end

这是我使用的迁移

➜  app  cat db/migrate/*
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end
class CreateFeeds < ActiveRecord::Migration
  def self.up
    create_table :feeds do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :feeds
  end
end
class CreateCasts < ActiveRecord::Migration
  def self.up
    create_table :casts do |t|
      t.string :name
      t.integer :feed_id

      t.timestamps
    end
  end

  def self.down
    drop_table :casts
  end
end
class CreateSubscriptions < ActiveRecord::Migration
  def self.up
    create_table :subscriptions do |t|
      t.integer :feed_id
      t.integer :user_id
    end
  end

  def self.down
    drop_table :subscriptions
  end
end

这篇关于Rails 模型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆