Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6750823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:51:14+00:00 2026-05-26T12:51:14+00:00

I need to add a select box next to each field in a create/update

  • 0

I need to add a select box next to each field in a create/update form which establishes its provenance. There is a table with photographer info, a table with sources, and a table establishing a many to many relation between them. A relation would take the id of a photographer, the column in that record (say, “last” for the lastname) and an id for the source where that information came from (the data in the table comes from different sources in a very fine-grained way).

What is the best way of adding a select box next to every field in the create/update form for a photographer so as to select where that info comes from?

I am using Ruby on Rails 3.0.3 with the mysql2 connector.

Right now this is the snippet I have to show a given column’s source (incomplete since it is missing a way to show the column name):

Source: <%= collection_select("photographers_sources","source_id",Source.all, :id, :name, {:include_blank => 'None'})%><br />

But I don’t know how to:

  1. pre-select the source whose id matches the many-to-many table’s source_id
  2. send that data back (column and source_id) and perform the association in the database

My schema:

ActiveRecord::Schema.define(:version => 0) do

  create_table "photographers", :force => true do |t|
    t.string  "first"
    t.string  "last"
    ...
  end

  create_table "photographers_sources", :force => true do |t|
    t.string  "photographer_column", :limit => 32, :default => "", :null => false
    t.integer "source_id",                                         :null => false
    t.integer "photographer_id",                                   :null => false
    t.string  "extra"
  end

  add_index "photographers_sources", ["photographer_id"], :name => "photographer_id"
  add_index "photographers_sources", ["source_id"], :name => "source_id"

  create_table "sources", :force => true do |t|
    t.string "name"
  end

end

My models:

class Photographer < ActiveRecord::Base
  validates :first,  :presence => true
  has_many :photographers_sources, :dependent => :destroy
  has_many :sources, :through => :photographers_sources

  def sourcelist
    PhotographersSource.where(:photographer_id => id)
  end
end

class Source < ActiveRecord::Base
  validates :name,  :presence => true
  has_many :photographers_sources, :dependent => :destroy
  has_many :photographers, :through => :photographers_sources
end

class PhotographersSource < ActiveRecord::Base
  belongs_to :photographer
  belongs_to :source
  validates :photographer_column,  :presence => true
  validates :source_id, :presence => true,
                        :numericality => { :only_integer => true }
  validates :photographer_id, :presence => true,
                        :numericality => { :only_integer => true }
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:51:15+00:00Added an answer on May 26, 2026 at 12:51 pm

    I added a form partial with the select box for the source:

    <%
    if @photographer.sourcelist.select{|s| s.photographer_column==column}.length != 0
        @id_to_select = @photographer.sourcelist.select{|s| s.photographer_column==column}[0].source_id
    else
        @id_to_select = 0
    end
    %>
    
    Source: <%= collection_select("photographers_sources_plain", column, Source.all, :id, :name, {:include_blank => 'None', :selected => @id_to_select })%><br />
    

    This is placed next to each field like so:

    <%= render (:partial => 'source_form', :locals => {:column => 'category'}) %>
    

    You would change “category” to whatever the field name is. I’m sure there must be a better way but that’s good enough for me.

    Note that the name generated in the select is “photographers_sources_plain[column]” so in the controller I have in the update action:

    params[:photographer][:photographers_sources] = []
    plain_sources = params[:photographers_sources_plain]
    plain_sources.each do |key, value|
      if value != ""
        params[:photographer][:photographers_sources] << PhotographersSource.new(:photographer_column=>key,:source_id=>value.to_i)
      end
    end
    

    An equivalent addition is needed for the new action. Once again, I’m sure there must be a better way but I’m new to Rails so this is what I could manage to do.

    This takes care of everything pretty well. Hope this helps someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add a custom field with the fb:request-form tag in facebook. ie:-
I need to add a new SELECT statement whose search condition include something like
I need to add the ability for users of my software to select records
On the select event of the datepicker I need to add and set values
Is there a simple way to add a styled line of small text next
I would like to create a select box with currently existing brands (e.g. Sony,
How to clear all the options in a select box in C# (Windows form
I have a select box, from which I want to remove selected choice with
I'm trying to create a select box drop down with a twist, Basically this
In my application, I have an HTML select box from which I remove and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.