2

I have a question about storing a value into a integer variable. Is it possible to store different id into the same variable ? This is my problem, I would like to use a collection_select to save many id's. My code is actually working for one variable, like this :

My code :

User model :

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

Pin model :

belongs_to :user

Pin controller:

 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 

Pin/new(views):

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

I would like something like that for my new views :

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

But the variables are not saving in my sql table. So my question is : That's possible to store many id in the same variable (:pin_maker) which is an integer ? Or should I create a new table for that ?

1
  • That's possible to store many id in the same variable (:pin_maker) which is an integer ? => no Commented Oct 6, 2016 at 15:30

2 Answers 2

2

pin_maker is user ? if so: (IMO): you need a n-to-n associations and a table users_pins.

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

On Rails:

Model User:

has_many pins, :through => :users_pins

Model Pin:

has_many users, :through => :users_pins
Sign up to request clarification or add additional context in comments.

Comments

0

Try has_many :users to involve your associations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.