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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:05:53+00:00 2026-06-08T20:05:53+00:00

I want to edit a 3 model on has_many. My question is about the

  • 0

I want to edit a 3 model on has_many. My question is about the controller and how can I access the information

I have the following model

Customer          Book           Book_Manager
id                id             id
first             description    customer_id
last                             book_id
email                            visible
password      

The relationship his has follow

Customer
  has_many book_managers
  has_many books :through -> book_managers
Book
  has_many book_managers
  has_many customer :through -> book_managers
Book_managers
  belongs_to :customer
  belongs_to :book

When a customer wants to edit the content, i want to be the latest one to be shown if any exist. The query i have at the moment doesn’t seem to handle an empty case and not sure how to do it.

@book = current_customer.books.order("created_at DESC").first

How should i declare it in a def edit of the customercontroller??

Update: I want to be able to see my data, unfoturnotly it doesn’t seem to work here my

customer controller

        def create
        @customer = Customer.new(params[:customer])
            @book = @customer.books.build(params[:book])
        if @customer.save
        cookies[:auth_token] = @customer.auth_token
        redirect_to @customer, notice: "Thank you for signing up!"
        else
            render "new"
        end
    end
    def edit
        @customer = Customer.find(params[:id])
        if current_customer.books.length > 0
           @book = current_customer.books.order("created_at DESC").first
        else
           #Nor books were found, so create one that has the proper relationship to current_customer
           @book = current_customer.books.build
        end
        end

I am rendering a partial from the book folder, should my create option be in the customer Controller or in bookControllers

Update: using customerController has my main create, when creating its say missing actions in books controller should i have more action in the book controller?

  • 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-08T20:05:55+00:00Added an answer on June 8, 2026 at 8:05 pm

    All you need to do is a simple check in the controller to see if any books exist. If one does not exists then create a new one.

    #Check if the books array contains anything. There are several ways to do this
    if current_customer.books.length > 0
       @book = current_customer.books.order("created_at DESC").first
    else
       #Nor books were found, so create one that has the proper relationship to current_customer
       @book = current_customer.books.build
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Invoice model which belongs to Customer (and of course Customer has_many
I have a variable length collection of address models which I want to edit.
So I have a model, let's call it Notes. On the notes, you can
I have a business model called Customer which has many required properties (via DataAnnotations)
I am following the Rails guide for doing multi-model nested form. I have 2
Here's my model hierarchy: Page has_many :media (media can be TextMedium , ImageMedium ,
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I have a Threads controller and Messages controller. Threads has_many Messages Once a user
I have a State model which has_many :cities and a city model which belongs_to

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.