简体   繁体   中英

How should I use the IF statement in Ruby on Rails

I have two tables: Persons and Statuses and have created two classes Person and Status . I am using following code to show the error message " Only one status is allowed ". The code is not working - I think there is some problem in my If statement.

          <ul>
              <% Person.all.each do |person| %>
                <li>
                  <%= person.name %>
                  <% if status.size >= 1 %>
                     <em>Only one status is allowed</em>
                  <% end %>

                </li>
              <% end %>
          </ul>

Table Persons

U_Id    Name    Place
  1    James    Florida
  2    Mark     California
  3    Steve    Newyork

Table Statuses

Id    Status    U_Id
 1    Hi          1
 2    OMG         2
 3    Bye         3
 4    Help me     2

Problem: Mark has posted 2 status his U_Id is 2 , I want to show him a error message like Only one post is allowed . How this can be done?

Update:

Person class

class Person < ActiveRecord::Base
  validates_presence_of :name
end

Your programming logic is incorrect. You are trying to impose a limit on the number of status messages a user can have, but it seems you are enforcing that limit too late, because you are printing an error message when the status is displayed rather than when it is submitted. The people viewing these messages are presumably other users and they hardly care if Mark violates your design constraints.

You have two options.


Limit the number of statuses one a user can have to one.

class User < ActiveRecord::Base
  has_one :status
end

This will allow you to do:

steve = User.find(3)
steve.status
=> "Bye"

Only display the last one

Alternatively, you can allow unlimited statuses, but only display the latest one.

class User < ActiveRecord::Base
  has_many :statuses
end

mark = User.find(2)
mark.statuses.last
=> "Help me"

On a side note... if users truly only have one status and the former statuses do not matter, then you should consider removing the status model and including the status as a string attribute on the user model. This will, in most cases, improve database performance.

in user.rb I admit you have has_many : statuses and in status.rb , belongs_to :user

<ul>
  <% Person.all.each do |person| %>
    <li>
      <%= person.name %>
      <% if person.statuses.size >= 1 %>
        <em>Only one status is allowed</em>
      <% end %>
    </li>
   <% end %>
 </ul>

but if you want a person to have only one status , why don't you check this when a status is created? or better, when he is trying to access the new action for status check if he has a status and redirect him back with a message saying "You have a status,you can have only one."

After this you can easy use:

user has_one :status

statuses_controller check:

def new
  if current_user.status.present?
    redirect to :back, error: "Only one status for user"
  else
    @status = current_user.status.new
  end
end

and when doing Person.all.each do |person| you can call directly person.status and it will be first and only one in db without needing to use an if statement. But this depends on how your app should work I guess.

I agree with Stas. status.size will not know what "status" is referring to. It seems like you are trying to refer to the status of each individual person, so you would need something like "person.status.size >= 1".

However, looking at your Person Class, it looks like you might not have the relationship set up yet. You need to include code in your Person class specifying that a Person has_many :statuses. So first do this and make sure that User.first.statuses works, then add the similar code to your views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM