首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >联想困境

联想困境
EN

Stack Overflow用户
提问于 2013-06-19 00:55:02
回答 2查看 80关注 0票数 0

在rails中实现pinterest (一组对象)中的棋盘之类的东西的最佳方式是什么?我正在尝试使用它,它看起来更像是一个数组实现。这是我的关联逻辑:用户有很多集合,用户有很多引脚,集合属于用户。

用户类

代码语言:javascript
复制
class User < ActiveRecord::Base
  has_many :pins, through: :collections
  has_many :collections  
end 

Pins类

代码语言:javascript
复制
class Pin < ActiveRecord::Base
 belongs_to :user
 has_many :collections 

end

Collection类

代码语言:javascript
复制
class Collection < ActiveRecord::base
  belongs_to :user
end

所以现在我的困惑是,如何实现一个控制器,它允许我创建一个集合,并在这个集合对象中创建或推送管脚,并将它们保存为current_user的另一个对象。希望我说得有道理

这是控制器

代码语言:javascript
复制
class CollectionsController < ApplicationController
   def create
     @collection = current_user.collections.new(params[:collection])
     #this where i'm confused , if it an array , how to implement it , to push or   create a pin object inside ?
   end 

end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-19 01:42:30

为此,您必须使用嵌套属性。

检查此http://currentricity.wordpress.com/2011/09/04/the-definitive-guide-to-accepts_nested_attributes_for-a-model-in-rails-3/

基本上你需要的是:

代码语言:javascript
复制
# collection model
accepts_nested_attributes_for :pins

# view, see also nested_form in github
f.fields_for :pins
票数 1
EN

Stack Overflow用户

发布于 2013-06-19 01:54:02

您要查找的是has_many_through协会。请参阅Rails指南中的第2.4节:http://guides.rubyonrails.org/association_basics.html

代码语言:javascript
复制
class User < ActiveRecord::Base
  has_many :collections  
end 

class Pin < ActiveRecord::Base
  has_many :collections, through: :pinnings
end

class Pinning < ActiveRecord::Base
  belongs_to :pin
  belongs_to :collection
end

class Collection < ActiveRecord::base
  belongs_to :user
  has_many :pins, through: :pinnings
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17174641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档