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

  • Home
  • SEARCH
  • 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 9237289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:30:13+00:00 2026-06-18T07:30:13+00:00

I’ve been trying to create web app in Ruby on Rails, which would collect

  • 0

I’ve been trying to create web app in Ruby on Rails, which would collect menu for different restaurants and show it in my app. I’d like to create many-to-many association, but I’m a little confused there. I’ve googled many-to-many relationship and read about HABTM or has_many/belongs_to, still a bit confused.
My classes:

restaurant = {name}

menu = {name, price}

And join table:

menu_restaurant = {date}

I’m confused here. I’ve read that I can’t use HABTM with extra values in join table. And also I should not have model for join table using HABTM.

My current models:
app/models/menu.rb

class Menu < ActiveRecord::Base
  attr_accessible :cenaStudent, :name
  has_many :restaurants, dependent: :destroy
  validates :priceStudent, presence: true
  validates :name, presence:true
end

app/models/restaurant.rb

class Restaurant < ActiveRecord::Base
  attr_accessible :name
  has_many :menus, dependent: :destroy

  validates :name, presence: true 
end

app/models/menu_restaurant.rb

class MenuRestaurant < ActiveRecord::Base
  attr_accessible :date
end

and these are my db tables
db/migrate/{timestamp}_create_restaurants.rb and similar {…}menu.rb

class CreateRestaurants < ActiveRecord::Migration
  def self.up
    create_table :restaurants do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :restaurants
  end
end

db/migrate/{timestamp}_create_menus_restaurants.rb

class CreateMenusRestaurants < ActiveRecord::Migration
  def up
    #create the association table
    create_table :menus_restaurants, :id => false do |t|
      t.integer :menu_id, :null => false
      t.integer :restaurant_id, :null => false
      t.date :date
    end

    #add index
    add_index :menus_restaurants, [ :menu_id, :restaurant_id]

  end

  def down
    remove_index :menus_restaurants, :column => [ :menu_id, :restaurant_id]
    drop_table :menus_restaurants
  end
end

What relationship in ruby on rails whould I use? What models do I need?

And I have a second question related RoR MVC architecture. I am going to write it here since it’s related.

I created a function in ruby that downloads pdf file containing menu list. Parses menu name and price. Restaurant name is matched with pdf file. So I have data I need to insert them into db. How do I do that and where? I guess some parts should be in controller and function should be in lib directory? It’s my first app using any architecture.
It’s also my first stackoverflow question, hope I didn’t forget anything.

  • 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-18T07:30:14+00:00Added an answer on June 18, 2026 at 7:30 am

    I think you are almost there, your migrations and models seem ok except for dealing with the join table :menu_restaurants.

    I have altered your models for so that the join table hooks everything up

    class Menu < ActiveRecord::Base
      attr_accessible :cenaStudent, :name
      has_many :menu_restaurants
      has_many :restaurants, through: :menu_restaurants , dependent: :destroy
      validates :priceStudent, presence: true
      validates :name, presence:true
    end
    
    class Restaurant < ActiveRecord::Base
      attr_accessible :name
      has_many :menu_restaurants
      has_many :menus, through: :menu_restaurants , dependent: :destroy
      validates :name, presence: true 
    end
    
    class MenuRestaurant < ActiveRecord::Base
      attr_accessible :date
      belongs_to :menu
      belongs_to :restaurant
    end
    

    Also I think your join table should be called menu_restaurants (singular on the word menu)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm trying to create an if statement in PHP that prevents a single post
I am writing an app for my school newspaper, which is run completely online
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.