【问题标题】:storing values into a integer variable将值存储到整数变量中
【发布时间】:2016-10-06 15:12:20
【问题描述】:

我有一个关于将值存储到整数变量中的问题。是否可以将不同的 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 表中。 所以我的问题是:有可能将许多 id 存储在同一个整数变量 (:pin_maker) 中吗?或者我应该为此创建一个新表吗?

【问题讨论】:

  • That's possible to store many id in the same variable (:pin_maker) which is an integer ? => 没有

标签: mysql sql ruby-on-rails ruby


【解决方案1】:

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

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

在轨道上:

模型用户:

has_many pins, :through => :users_pins

型号别针:

has_many users, :through => :users_pins

【讨论】:

    【解决方案2】:

    尝试 has_many :users 让你的关联参与进来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多