1

こんにちは仲間の開発者!最近、私は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があると便利です。非常に単純ですが、私が達成しようとしていることの移行を作成するのにも苦労しています。

上記のコードが機能しないことはわかっています-私はそれで遊んでいるので、これは私が達成しようとしていることの概念にすぎません。

基本的に私の質問は次のとおりです:どういうわけか結合モデルを介してそれを行うべきですか、それともスコープを使用するべきですか?(コードスニペットを与えることもできます)そしてそのための移行をどのように行うのですか?

おかげで、申し訳ありませんが、この単純なケースに関する多くの情報をWeb上で見つけることができませんでした。

編集:has_and_belongs_to_many on User and Feedは、私の場合は機能しません。これは、@ user.castsを使用できないため、@user.feedsと@feed.usersのみを提供するためです。

4

2 に答える 2

2

必要なのは、ユーザーとフィードの間の多対多の関係です。

ユーザーとフィードの関係を機能させるには、コードにこのようなものが必要です。

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で説明)とともに使用することも検討してください。ユーザーフィード関係レコードのメタデータを保存するのが好きです。

編集: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
于 2011-06-11T21:51:35.063 に答える
0

http://railscasts.com/episodes/265-rails-3-1-overviewで、Ryan Batesは、rails 3.1は連鎖されたhas_many:through呼び出しをサポートしていると述べています。3.0では不可能です

于 2011-06-11T23:17:30.410 に答える