开发者

Soliciting Rails Association Feedback + Questions from a Noob

Preface:

If you hang out in #rubyonrails on freenode this may sound like an echo to you as i asked it in there 2 days ago. After spending a number of hours researching AR associations, following my discussions in #rubyonrails, i still feel lost so I'm asking here. :)

Goal

I host a number of blogs. My intent is to create batch submissions where i take a Post, select the blogs I'd like the Post to be Submitted to, then give the Batch a submission date. Later on I'd like to create a Worker that queries the Batches and then actually submits the Post to it's associated Blogs.

The Confusion

I am confused about how I should setup my associations and the corresponding tables.

Here is what i have for Models.

class Blog < ActiveRecord::Base
  has_many :submissions
  has_many :posts, :through => :submissions
end


class Post < ActiveRecord::Base
  has_many :submissions
  has_many :blogs, :through => :submissions
end


class Submission < ActiveRecord::Base
  belongs_to :post
  belongs_to :blog
end

Here is what i have for Tables.

[Blogs]
id         :integer         not null, primary 开发者_StackOverflow中文版key
title      :string(255)
etc

[Posts]
id         :integer         not null, primary key 
subject    :string(255)
content    :text
etc

[Submissions]
id          :integer         not null, primary key
submit_date :datetime
post_id     :integer
forum_id    :integer
etc

Questions

  • I currently have no Batch model -- should i have this? Or can a "batch" be inferred by backtracing submissions?
  • If i should have a Batches model, would that mean the Submisson model then gets an additional foreign key?
  • Is there a more "rails way" to do this?
  • Any other input is welcome!

Thanks a lot for reading this gigantic post.


In your submissions table, I'd suggest you change forum_id to blog_id to conform with the Rails ethos convention over configuration. Otherwise you'll need to specify that forum_id is the foreign_key for submissions.

So either [preferred]:

[Submissions]
id          :integer         not null, primary key
submit_date :datetime
post_id     :integer
**blog**_id    :integer
etc

Or:

class Blog < ActiveRecord::Base
  has_many :submissions, :foreign_key => "forum_id"
  has_many :posts, :through => :submissions
end

Based on your description, I don't believe you need another model rather just use the submit_date field on the submissions table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜