简体   繁体   中英

Button to store data from one table to another in rails

I have a funding table in which there is a field name_of_organisation which displays a list of organisations from organisation table. In the list I have an option "Not Listed". If notlisted is selected a further form opens to add organisation. But that organisation details get added in the funding table. What I want is on funding show page if new organisation details are added admin can verify the organisation details and should have a button to click on so that all those organisation details in funding table get added in the organisation table. I need help with query for add organisation button on show.html.erb page for funding to add organisation details from funding to organisation table.

schema for funding table

create_table "fundings", force: :cascade do |t|
    t.string "type_of_activity"
    t.string "season"
    t.text "activity_details"
    t.string "name_of_organisation"
    t.string "city"
    t.string "province"
    t.string "postal_code"
    t.string "telephone_number"
    t.text "address"
    t.integer "organisation_id"
    end

schema for organisation table

create_table "organisations", force: :cascade do |t|
t.string "name_of_organisation"
t.text "address"
t.string "city"
t.string "province"
t.string "postal_code"
t.string "telephone_number"
end

Show.html.erb (funding)

<%unless @funding.organisation.blank?%>
    <p><strong>Name of the Organisation:</strong>
    <%= @funding&.organisation&.name_of_organisation %></p><br>
  <%end%>

<% if @funding.name_of_organisation.present? %>
 <%= @funding.name_of_organisation %>
  <% if current_user.superadmin? %>
    <%= link_to 'Add Organisation', '' %>
  <% end %>
<% end %>

You should use the ID of the organization instead of the name to link the two models together. Thats how associations in ActiveRecord work.

Your attempt has a huge flaw in that editing the name of the organization would break any assocation. IDs don't change.

Start by generating a migration to add a foreign key column to the fundings table:

# rails g migration add_organization_to_fundings organization:belongs_to
how class AddOrganizationToFundings < ActiveRecord::Migration[5.0]
  def change
    add_reference :fundings, :organization, foreign_key: true
  end
end

If you have any existing data that is linked by the name you need to iterate through and fix these records:

Funding.find_each(batch_size: 100) do |f|
  organization = Organization.find_by(name: f.name_of_organisation)
  f.update!(organization_id: organization.id)
end

You should then write a migration to remove the fundings.name_of_organisation column.

You then need to setup the proper assocations:

class Funding < ApplicationRecord
  belongs_to :organization
end

class Organization < ApplicationRecord
  has_many :fundings
end

You can then create a select tag by using the form option helpers:

<%= form_for(@funding) do |f| %>
  <%= f.select :organization_id, Organization.all, :id, :name %>
<% end %>

I know it's infuriating when you ask "How do I do this?" on stackoverflow and they say "Don't", but in this instance it's pretty cut and dry. Don't do it this way.

You're storing the organization name and address and such twice, once for the funding and once for the organization. This is a very bad practice, which will bite you when it comes time to change anything at all. You want to store the organization details the organization table, and that table only. Strip it out of your funding table, and add a foreign key to reference which organization it belongs to.

These are pretty basic concepts, you really want to take a little time and learn about databases before going further. Find a decent lecture, article, or tutorial on "relational database basics" and work through it. It doesn't have to be (and probably won't be) specific to rails.

After that, read up on Activerecord , especially relations.

To solve your admin approval problem, what I would do is add an "approved" boolean flag to the organization table and a custom controller action only available to admins to approve it. You can go further and create an "approved" scope for normal users, and a "pending" scope or something visible to admins. That's only one possible solution of many, though.

为了“添加组织”,我将运行以获取您尝试执行的逻辑方法的路径并进行remote:true根据您打算使用js渲染还是重定向到新屏幕,对逻辑进行remote:true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM