如果一个对象有多个belongs_to,我该如何创建它?

发布于 2024-10-31 17:41:33 字数 548 浏览 0 评论 0原文

我有以下内容:

class Org < ActiveRecord::Base 
  has_many :users              
  has_many :entries          
end

class Entry < ActiveRecord::Base
  belongs_to :org              
  belongs_to :user             
  validates_presence_of :entry_text
end

class User < ActiveRecord::Base
  belongs_to :org              
  has_many :entries          
  validates_uniqueness_of :user_name
  validates_presence_of :user_name, :length => { :minimum => 3 }
end

我可以创建组织和用户...如果有两个belongs_to,我如何创建一个条目?这种模式叫什么?

I have the following:

class Org < ActiveRecord::Base 
  has_many :users              
  has_many :entries          
end

class Entry < ActiveRecord::Base
  belongs_to :org              
  belongs_to :user             
  validates_presence_of :entry_text
end

class User < ActiveRecord::Base
  belongs_to :org              
  has_many :entries          
  validates_uniqueness_of :user_name
  validates_presence_of :user_name, :length => { :minimum => 3 }
end

I can Create Orgs and Users... How do i create an entry if there are two belongs_to? and what is this pattern called?

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

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

发布评论

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

评论(2

绅士风度i 2024-11-07 17:41:33

双重嵌套资源很棘手。用户的技巧通常是将其排除在您想要的进入路径之外。

您的问题有点宽泛,但如果您指定更多信息,人们将能够更好地帮助您。另外,我建议您使用 gem Devise 作为您的用户管理系统。由于您使用的是“用户”,我假设您希望组织中的用户创建条目。创建的条目将是组织的一部分,用户将是会话的当前用户。抱歉,如果我的假设是错误的。

你的routes.rb文件看起来像这样(假设rails 3):

resources :orgs do
资源:条目
然后

你的入口控制器的创建看起来像:

@entry = @org.entries.new(params[:topic])
@entry.user = current_user #or however you are managing the current user's session.

你想要通过创建一个加载当前组织并执行 before_filter :loadOrg 的方法来为整个类设置组织

def loadOrg
   @org = Org.find(params[:id])
end

这当然假设你的路径是这样的: /org/(id)/entry/(entry_id)

而不是

/org/(id)/user/(user_id)/entry/(entry_id) ,

在我看来这是不必要的,并且可能会导致更多问题。您始终可以创建一个用户页面模型来调用用户的所有条目,但默认路由不一定必须在路径中包含用户。

Double nested resources are tricky. The trick with users usually is to keep it out of your desired entry path.

Your question is kind of broad, but if you specify more information, people would be able to help you better. Also, I would recommend using the gem Devise for your user management system. Since you're using 'users' I would assume you want users from orgs to create entries. The entry created would be a part of org and the user would be the session's current user. Sorry if I am wrong to assume this.

Your routes.rb file can look something like this (assuming rails 3):

resources :orgs do
resources :entries
end

Then the create of your entry controller would look like:

@entry = @org.entries.new(params[:topic])
@entry.user = current_user #or however you are managing the current user's session.

And you'd want to set the org for the entire class by making a method that loads your current org and do a before_filter :loadOrg

def loadOrg
   @org = Org.find(params[:id])
end

This is of course assuming your path is something like: /org/(id)/entry/(entry_id)

and not

/org/(id)/user/(user_id)/entry/(entry_id)

which in my opinion is unnecessary and can lead to more problems. You can always create a userpage model that calls all entries by users, but the default route doesn't necessarily have to include users in the path.

记忆之渊 2024-11-07 17:41:33

我没有看到任何问题。

@entry = Entry.create(:entry_text => "Hello World!")

现在问题是澄清您需要什么:

  • @entry 可以同时属于 orguser 吗?或者它只能属于其中之一?
  • @entry 应该至少属于其中之一吗?

如果@entry应该只属于其中之一,那么你应该使用多态性

http://railscasts.com/episodes/154-polymorphic-association< /p>

class Entry < ActiveRecord::Base
  belongs_to :textable, :polymorphic => true
  validates_presence_of :entry_text
end

class Org < ActiveRecord::Base 
  has_many :users              
  has_many :entries, :as => :textable       
end

class User < ActiveRecord::Base
  belongs_to :org              
  has_many :entries, :as => :textable         
  validates_uniqueness_of :user_name
  validates_presence_of :user_name, :length => { :minimum => 3 }
end

I don't see any problem.

@entry = Entry.create(:entry_text => "Hello World!")

Now questions to clarify what do you need:

  • Can @entry belongs both org and user at the same time? Or it can belongs to only one of them?
  • Should @entry belongs to at least one of them?

If @entry supposed to belong only one of them, so you should use Polymorphism

http://railscasts.com/episodes/154-polymorphic-association

class Entry < ActiveRecord::Base
  belongs_to :textable, :polymorphic => true
  validates_presence_of :entry_text
end

class Org < ActiveRecord::Base 
  has_many :users              
  has_many :entries, :as => :textable       
end

class User < ActiveRecord::Base
  belongs_to :org              
  has_many :entries, :as => :textable         
  validates_uniqueness_of :user_name
  validates_presence_of :user_name, :length => { :minimum => 3 }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文