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 8994933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:02+00:00 2026-06-15T23:32:02+00:00

Beginner question here, any help much appreciated. I have a table of users and

  • 0

Beginner question here, any help much appreciated. I have a table of users and a table of products and a join table called products_users. I can successfully link a product to a user in the command line by doing this:

u = User.where(:state => 'Oregon').first
u.products << Product.where(:search_location => 'Portland')

My first question is how would I link the Portland product to ALL users that have the state of Oregon and not just one user? If I don’t add the .first it doesn’t work.

But my main question is how to get the app to do this instead of me doing it in the command line? For instance, I have a table that contains 10,000 users and 1000 of them are :state => ‘Oregon’ so how do I create an admin page where I can type Oregon into the state field and then type Portland into another field and the appropriate entries are linked together and added into the join table?

UPDATE

Using siddick’s code I’ve come up with this:

admin.rb

def link_product_to_user
products = Product.where(:search_location => params[:location])
User.where(:state => params[:state]).find_each do |user|
  user.products << products
end
  end

admin_controller.rb

def index    
@link = link_product_to_user
end

admin/index.html.erb

<%= form_for(@link) do |f| %>
<div class="field">
  <%= f.label :location %><br />
  <%= f.text_field :location %>
</div>
<div class="field">
  <%= f.label :state %><br />
  <%= f.text_field :state %>
</div>

<div class="actions">
  <%= f.submit %>
</div>

which gives the following error:

undefined local variable or method `link_product_to_user' for #<AdminController:0x007fd8e899bf10>

How should I define the method so it can be found? And is using form_for(@link) the correct way to submit the values to the method? thanks

  • 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-06-15T23:32:04+00:00Added an answer on June 15, 2026 at 11:32 pm

    You can use find_each to process record one by one.

    def your_action
      products = Product.where(:search_location => params[:location])
      User.where(:state => params[:state]).find_each do |user|
        user.products << products
      end if products.exists?
      ... Your code ...
    end
    

    Index View file can be:

    <%= form_tag(:action => "link_product_to_user") do %>
      <div class="field">
        <%= label_tag :location %><br />
        <%= text_field_tag :location, params[:location] %>
      </div>
      <div class="field">
        <%= label_tag :state %><br />
        <%= text_field_tag :state, params[:state] %>
      </div>
      <div class="actions">
        <%= submit_tag %>
      </div>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another beginner's question that hopefully someone can help with. I'm trying to
Bit of a beginner question here: Say I have a block of xml: <root>
I have a question about ocaml, i'm a beginner :-) Here is an example
Really beginner question here. I have an NSArray of images which in its last
Beginner here, I have a simple question. In Android what would be the best
I guess that's a beginner question, but i can't find any solution or best
Probably beginner jquery question... Here is my problem: I have dynamically created list of
Hey, a beginner's question here.... In order to learn PHP and SQl I have
As I'm a beginner, here's another question about D3.js. I have constructed a force
Sorry in advance for this being such a beginner question. Here are the steps

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.