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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:52+00:00 2026-05-24T11:27:52+00:00

I have a Location model with fields: Location(id: integer, name: string, address: string, latitude:

  • 0

I have a Location model with fields:

Location(id: integer, name: string, address: string, latitude: decimal, longitude: decimal, created_at: datetime, updated_at: datetime, rating: decimal, rating_count: integer)

In my location model, i have the following validations (which work fine):

attr_accessible :name, :address, :latitude, :longitude, :rating, :rating_count

validates :name, :presence => true, :length => { :maximum => 50 }
validates :address, :presence => true
validates :rating, :inclusion => 0..5
validates :rating, :presence => { :message => " cannot be blank" }

I also have a Post model with fields:

Post(id: integer, user_id: integer, location_id: integer, info: string, created_at: datetime, updated_at: date time)

And the following validations:

attr_accessible :info, :address, :name, :rating
belongs_to :user
belongs_to :location
attr_accessor :rating, :address, :name #Virtual attributes for the Post create form

validates :name, :presence => true, :length => { :maximum => 50 }
validates :address, :presence => true
validates :rating, :inclusion => 0..5
validates :rating, :presence => { :message => " cannot be blank" }

Now, the issue is that when I try to create a new Post, the validations for name and address work fine (just as they work for Location), but the validation for rating always fails. Even when I enter a rating of ‘3’, I get the error Rating is not included in the list for some reason, although a rating of ‘3’ is validated correctly if used directly with the Location#create action.

Does anyone know why only the rating validation isn’t working as expected with Post, even though it works fine with Location, and the other validations behave identically with the two models ?

EDIT: Here’s the create action from posts_controller:

def create
@post = current_user.posts.build(params[:post])
@location = Location.find_by_address_and_name(params[:post][:address], params[:post][:name])

if @location.nil?
  @location = Location.find_by_address(params[:post][:address])
end

#If the address and the name of the location is the same, just update the rating for that location, and associate the post with the location
if (@location && params[:post][:name] == @location.name)
  @post.location_id = @location.id
  @location.rating_count += 1
  @location.rating = ( (@location.rating + params[:post][:rating].to_r.to_d )/ @location.rating_count )
else
  @post.location_id = Location.create(:address => params[:post][:address], :name => params[:post][:name], :rating => params[:post][:rating].to_r.to_d).id
end

@post.save


respond_to do |format|
  if @post.save
    @location.save! #Update the location only if the post was successful
    format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    format.xml  { render :xml => @post, :status => :created, :location => @post }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
  end
end
end 

I know this is a LARGE post, but the main point is, 2 identical validations are failing in one place and succeeding in another. One of them validates attributes which are part of the database, the other validates virtual attributes, and I suspect this is the underlying problem.

  • 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-05-24T11:27:52+00:00Added an answer on May 24, 2026 at 11:27 am
    validates :rating, :inclusion => ['0', '1', '2', '3', '4', '5']
    # OR
    validates :rating, :inclusion => (0..5).map(&:to_s)
    

    UPD

    add this method

    def rating=param
      @rating = param.to_f
    end
    

    so now you can use your original validation

    validates :rating, :inclusion => (0..5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have given a location defined by latitude and longitude. Now i want to
I have a User model with the usual information (login, email, name, location, etc).
I have table with 50 entries (users with such details like Name Surname Location
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two columns in a location model that should both associate with the
I have a model with TPT inheritance. Location ( abstract ) Street ( derived
I have this rewrite rule RewriteEngine On RewriteBase /location/ RewriteRule ^(.+)/?$ index.php?franchise=$1 Which is
We have a JavaScript function named move which does just windows.location.href = any given
I have a Powershell script that copies files from one location to another. Once
I have 2 machines, one at Location A and the other at Location B.

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.