2016-10-06 128 views
2

我有一个关于将值存储到整数变量的问题。是否可以将不同的ID存储到同一个变量中? 这是我的问题,我想用一个collection_select来保存很多ID。 我的代码实际上是工作的一个变量,像这样:将值存储到整数变量

我的代码:

用户模式:

has_many :pins 
    scope :artist, -> { where(artist: true) } 

针型号:

belongs_to :user 

引脚控制:

def new 
    @pin = Pin.new 
    @users = User.all.artist 
    end 

    def create 
    @pin = current_user.pins.build(pin_params) 
    if @pin.save 
     redirect_to @pin, notice: "successfully created!" 
    else 
     render 'new' 
    end 
    end 

引脚/新(视图):

<div class="form-group"> 
    <%= f.collection_select(:pin_maker, @users, :id, :pseudo) %> 
    </div> 

我想类似的东西为我的新观点:

<div class="form-group"> 
    <%= f.collection_select(:pin_maker, @users, :id, :pseudo, { }, {:multiple => true}) %> 
    </div> 

但变量不是在我的SQL表保存。 所以我的问题是:这是可能存储在同一个变量(pin_maker),这是一个整数多个ID?或者我应该为此创建一个新表格?

+0

'这是可以存储许多ID在同一个变量(: pin_maker)这是一个整数?'=> no –

回答

0

尝试has_many:用户涉及您的协会

+0

不是哥们,它不工作 – tutoto

2

pin_maker是用户?如果是这样的话: (IMO):你需要一个n对n的关联和一个表users_pins。

Logic: 
One user has many pins 
One pin may be made by many users 

on Rails的:

型号用户:

has_many pins, :through => :users_pins 

型号引脚:

has_many users, :through => :users_pins