Rails 模型关系

发布于 2024-11-15 00:59:26 字数 800 浏览 5 评论 0原文

各位开发者大家好! 最近我一直在玩 Rails 3.0,经过大量研究后我有点陷入困境。 我想知道什么方法或解决方案最适合我的情况(我还找不到答案)。 所以我想要实现的目标是简单而直接的。

我想做这样的事情:

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 也很好。非常简单,对吧,但我也很难为我想要实现的目标创建迁移。

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

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

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

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

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.

I want to do something like this:

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

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.

Edit: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-11-22 00:59:26

您想要的是用户和提要之间的多对多关系。

您需要在代码中使用类似的内容,以使用户和提要关系发挥作用。

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

您可能还想查看使用has_many :through 带有一个中间模型(此处解释 http://guides.rubyonrails.org/association_basics.html#choosing- Between-has_many-through-and-has_and_belongs_to_many)如果您想存储用户的任何元数据 - Feed 关系记录。

编辑:我设法在 3.0 和 3.1 上获得类似的设置(使用 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

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

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

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.

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

Here are the contents of my models.

➜  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

and here are the migrations I used

➜  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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文