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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:46:36+00:00 2026-05-21T06:46:36+00:00

I have a users model and a book model. Users can read books (as

  • 0

I have a users model and a book model. Users can read books (as a reader) which creates an entry in the Readings model:

id | reader_id | book_id

Users also have a list of books that they have read. These are stored in the Red (I use Red because the present and past tense of the word ‘read’ are the same) model which looks the same as the Reading model above.

Now when a user is reading a book, I would like to display a button which represents finishing the book.

The finish action is in the ReadingsController and looks like this:

def finish
  @book = current_user.readings.find(params[:id]).book
  current_user.stop_reading!(@book)
  current_user.make_red! @book

  redirect_to :back
end

As you can probably tell, this takes in the id of a record in the readings table, destroys it and makes a new record in the table for recording books red.

The form helper for the “Finish Reading” button currently looks like this:

<%= form_for :reading, current_user.readings.find_by_book_id(book.id), :url => { :controller => :readings, :action => "finish" }, :method => :delete do |f| %>
  <div class="actions"><%= f.submit button_text %></div>
<% end %>

But for some reason this renders a form with the wrong id because “9781440506604” is not the id of a record in the readings table, it’s the id of a record in the books table (the ISBN-13 of a book to be precise).

<form accept-charset="UTF-8" action="/readings/9781440506604/finish" method="post">
</form>

What is it I’m doing wrong?

EDIT to add reading.rb

class Reading < ActiveRecord::Base
  attr_accessible :book_id

  # one person reading a new book may cause feed_item creations in multiple users feeds
  has_many :feed_items, :as => :event
  has_many :comments, :as => :parent, :dependent => :destroy

  scope :from_users_followed_by, lambda { |user| followed_by(user) }

  # need to pass the class name here because there is no Reader model
  belongs_to :reader, :class_name => "User"
  belongs_to :book

  validates :reader_id, :presence => true
  validates :book_id, :presence => true

  def self.followed_by(user)
    ...
  end
end

# and user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :avatar, :remember_me, :avatar_url
has_many :readings, :dependent => :destroy,
                      :foreign_key => "reader_id"
  has_many :reads, :through => :readings, :source => :book
  has_many :reds, :foreign_key => "reader_id",
                  :dependent => :destroy
  has_many :red, :through => :reds, :source => :book

  def reading? book
    self.readings.find_by_book_id(book)
  end

  def read! book
    self.readings.create!(:book_id => book.id)
  end

  def stop_reading! book
    self.readings.find_by_book_id(book).destroy
  end

  def red? book
    self.reds.find_by_book_id(book)
  end

  def make_red! book
    unless red? book
      self.reds.create!(:book_id => book.id)
    end
  end
end

By the way I tried making a user who is reading book 1 and doing user.readings.find_by_book_id(1) in the console and it returns a record from the readings table.

as requested

# routes.rb
resources :readings, :only => [:create, :destroy, :show] do
  member do
    post :create_comment
    delete :finish
  end
end
  • 1 1 Answer
  • 2 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-21T06:46:37+00:00Added an answer on May 21, 2026 at 6:46 am

    Looks like you have got to_param method in your Reading model

    try to call id clearly:

    current_user.readings.find_by_book_id(book.id).id
    

    UPD

    1. remove :only => [:create, :destroy, :show] from your routes
    2. use this <%= form_for :reading, current_user.readings.find_by_book_id(book.id), :url => { :controller => :readings, :action => "finish", :id => current_user.readings.find_by_book_id(book.id).id }, :html => {:method => :delete} do |f| %>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model Book which can be added to user's Favorites. So I
I have 2 models: User and Book and a join model ownership which connect
I have a list_users model with fields (id, list_id, user_id) which joins Users w
I have a author model and a books model. A user can modify properties
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a model like this: class Users(db.Model): email = db.EmailProperty(required=True, indexed=True) user_name =
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
User is a model. I have 100 users. I need to present the list
For instance, let's say I have a User model. Users have things like logins,

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.