0

I have these two tables users from devise and border_rot_imports. In the border_rot_imports table, I have:

Table "public.border_rot_imports"

issuing_officer_id  | integer
*****

Foreign-key constraints:
    "fk_rails_6d0f78c8a8" FOREIGN KEY (issuing_officer_id) REFERENCES users(id)

and under users table i got :

Referenced by:
Table "public.users"
*******
    TABLE "border_rot_imports" CONSTRAINT "fk_rails_6d0f78c8a8" FOREIGN KEY (issuing_officer_id) REFERENCES users(id)

MODELS

class BorderRotImport < ApplicationRecord
    belongs_to :users, foreign_key: :issuing_officer_id
end

class User < ApplicationRecord
     has_many :issuing_officer, class_name: 'BorderRotImport'
end

so now the question is, how do i gain access to all the fields under the user table from border_rot_imports.

so under my new.html.erb I have a form that stores data to the database. And my create function is as follows:

def create
    @border_rot_import = BorderRotImport.new(border_rot_import_params)
    @border_rot_import.time_in = Time.current
    @border_rot_import.date_in = Time.current
    @border_rot_import.issuing_officer_id = User.find(current_user).id

    #setting default values
    @border_rot_import.vin = 'N/A' unless @border_rot_import.vin? 
    @border_rot_import.license_plate_in = 'N/A' unless @border_rot_import.license_plate_in?

  #  respond_to do |format|
  #    if @border_rot_import.save
  #      format.html { redirect_to @border_rot_import, notice: "Border rot import was successfully created." }
  #      format.json { render :new, status: :created, location: @border_rot_import }
  #    else
  #      format.html { render :new, status: :unprocessable_entity }
  #      format.json { render json: @border_rot_import.errors, status: :unprocessable_entity }
  #    end
  #  end
  end

now the database has the id of the user, but now i need the username through @border_rot_import so that I can display it in a table in the index instead of the user id. If there is another approach please provide me with some links.

Thank you in advance.

0

2 Answers 2

1

First off your assocations don't make sense. A belongs_to assocation should always have a singular name and has_many should be plural.

has_many :issuing_officer, class_name: 'BorderRotImport'

Doesn't make any sense at all either besides the pluralization issue. When you call user.issuing_officer its going to do:

JOINS border_rot_imports ON border_rot_imports.issuing_officer_id  = users.id

And you get an ActiveRecord::Relation of BorderRotImport instances - WOOT?!

What you actually want is most likely something like this:

class BorderRotImport < ApplicationRecord
  belongs_to :issuing_officer, class_name: 'User'
end
class User < ApplicationRecord
  has_many :border_rot_imports
end

While you can call - @border_rot_import.issuing_officer.name you should consider using delegation to remove the law of demeter violation:

class BorderRotImport < ApplicationRecord
  belongs_to :issuing_officer, class_name: 'User'
  delegate :name, :foo, :bar, :baz,
    to: :issuing_officer,
    prefix: true # optional
end
@border_rot_import.issuing_officer_name

You also should consider setting the defaults in the model/table instead of bloating the controller:

class BorderRotImport < ApplicationRecord
  belongs_to :issuing_officer, class_name: 'User'
  delegate :name, :foo, :bar, :baz,
    to: :issuing_officer,
    prefix: true # optional

  # Why do you even need two separate attributes here? 
  attribute :time_in,         :time,      default: ->{ Time.current }
  attribute :date_in,         :datetime,  default: ->{ Time.current }

  attribute :vin,             :string,    default: 'N/A'
  attribute :license_plate_in :string,    default: 'N/A'
end

Or at least just pass a block and use conditional assignment:

def create
  @border_rot_import = current_user.border_rot_imports.new(border_rot_import_params) do |import|
    import.time_in = Time.current
    import.date_in = Time.current
    import.vin ||= 'N/A'
    import.license_plate_in ||= 'N/A'
  end
  # ...
end
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try referencing the related object from your instance variable? Normally, you can use:

@instance.related_object.attribute_name

where @instance is @border_rot_import, related_object is the model that shares a relation with the BorderRotImport model (user in your case) and attribute_name is the attribute of User which you wish to display.

<%= @border_rot_import.user.name %>

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.