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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:27:11+00:00 2026-06-08T21:27:11+00:00

I have a User that can have many Restaurants . I can also have

  • 0

I have a User that can have many Restaurants. I can also have multiple users.

I’d like to have it so that if User A creates Restaurant A, he should NOT be able to make another restaurant with the same name.

However, if User B goes to create Restaurant A, that should be allowed but still cannot make another Restaurant A afterwards.

I have the following has_many through relationship:

restaurant.rb

has_many :ownerships
has_many :users, :through => :ownerships

# This following ensures uniqueness of the name within the 
#   Restaurants table regardless of who the User is that created it.
validates :name, presence: true, uniqueness: true

user.rb

has_many :ownerships
has_many :restaurants, :through => :ownerships

ownership.rb

belongs_to :restaurant
belongs_to :user

What I’ve Tried

1. Adding :uniqu => true

I’ve tried adding :uniq => true to the restaurant.rb file so it looks like this:

has_many :ownerships
has_many :users, :through => :ownerships, :uniq => true

And removing uniqueness: true from the validation so it looks like this:

validates :name, presence: true

But that doesn’t do anything useful.

2. Adding validation within ownership.rb

I’ve tried adding the validation to the ownership.rb file as such:

validates :restaurant, uniqueness: {:scope => :user}

But I get:

NoMethodError in RestaurantsController#create
undefined method `text?' for nil:NilClass

And I can’t seem to tell it to look for the restaurant name within the scope of user either within this validation.

3. Creating before_create callback function

In my restaurant.rb file, I declared the following:

before_create :check_uniqueness

def check_uniqueness?
  user = User.find_by_id(self.user_ids)

  isUnique = false
  user.restaurants.each do |restaurant|
    if !Restaurant.find_by_name(self.name).nil? # Restaurant w/ same now found
      isUnique = false
    else
      isUnique = true
    end
    return isUnique
  end
end

My assumption is that before the restaurant record is created, it’ll do this check_uniqueness check and if the function returns false, it’ll not save.

But I’m getting the following error when I hit the submit button:

NameError in RestaurantsController#create
undefined local variable or method `check_uniqueness' for #<Restaurant:0x007f95a16d10f8>

Working Solution

Thanks to Robert Chuchro’s help below, I was able to get the validation to work. Here’s what I did:

restaurant.rb

before_create :unique_per_user?

def unique_per_user?
  user = User.find_by_id(self.user_ids)
  restaurant = user.restaurants.find(:all, :conditions => ["name = ?", self.name])

  if restaurant.size > 0
    self.errors.add(:name, ": You've already created a restaurant with this name.")
  end

  return (restaurant.size <= 0)
end
  • 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-08T21:27:12+00:00Added an answer on June 8, 2026 at 9:27 pm

    You can try to define a method to do this in your restaurant model

    def unique_per_user?
      #get user trying to create restaurant, either by paramter or association
      #check if any of the user's current restaurant names match this name (return true/false)
    end
    

    now whereever you define a new restaurant check if its unique_per_user? before deciding to save it.

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

Sidebar

Related Questions

I have a mathparser that can do functions like IntPow(3,2). If a user pastes
I have a Users model that can have many Groups through Memberships. If a
I have a users model that can have many holidays through a rich join
I have a user table that has many columns, it looks roughly like this:
I have the following: User model that has_and_belongs_to_many Restaurants and vice-versa. Restaurant model that
I have an action that a user can do many times a day. I'm
I have a datagrid that potentially can have many rows. As the user right
I have some users that can have many posts , and each of those
I have this scenario User can have many profiles per year and i also
I have created a User called Student and Teacher that can login into the

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.