Validate in a model using a controller def

Hi

I hope someone can help me with this

I have a model Question, and its controller .. what I've done to validate that there's no evaluation with 2 question with the same number is

          validates_uniqueness_of :qnumber, :scope => :evaluation_id

but when I try to update a question's content I got an error message that says that "#{qnumber} has already been taken" and that means that the validation works, but not as I want

Can I obtain wich "def" is using ? because I want to do this - Question Model

           IF ("def create" or something)                  do the validation            elsif ("def update")                  don't do it           end

I hope someone can help me

Thanks in advance

I don't know what you mean by "def" but if you want to do conditional validations have a look at the Rails Guide on validations and it will show you how.

Colin

What I mean by "def" is the basic CRUD that rails provide

def create .... end

def update ... end

def new ... end

I want that the data validates in 2 cases (create / update) because when I want to update , for example:

Evaluation 1 - Question 1

If I update question 1 and change the content, and then I save it shows a error message that says "The number is being used" (and that shows that validates_uniqueness_of :qnumber, :scope works) I want that it only validates it when I'm CREATING a new one, not when I'm UPDATING

How can I do that?

That is not really a function of the controller action, it is just whether you are creating a record or updating an existing one. Did you read the guide as I suggested?

Colin

I'm doing it :slight_smile: but I read in another place, that its not safe.. or not recommended to access a controller from a model is that true?

Those def were created when I made rails g scaffold

I'm doing it :slight_smile: but I read in another place, that its not safe.. or not recommended to access a controller from a model is that true?

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate points in previous message. Thanks.

As I said you do not need to access the controller. It is entirely within the model. The model knows whether it is being asked to create a new record or to update an existing one. Look at section 4.4 in the guide (:on)

Colin

I want that the data validates in 2 cases (create / update) because when I want to update , for example:

Evaluation 1 - Question 1

If I update question 1 and change the content, and then I save it shows a error message that says "The number is being used" (and that shows that validates_uniqueness_of :qnumber, :scope works) I want that it only validates it when I'm CREATING a new one, not when I'm UPDATING

validations are fundamentally ignorant of what it is that has caused save to be called (i.e. it doesn't know what action, controller etc. There might not even be a controller if the code was being run from a background task).

The validation methods do take an on option (:on => :create or :on => :update) but you should understand that this refers to whether active record is create or updating an object and has nothing to do with what controller action was involved.

However, validates_uniqueness_of should already be adding a condition to the query so that when updating an existing object it doesn't find itself, so I'm not sure why you're running into this

Fred

Hi Colin. I asked about this before in this list. There are no preferences in this list, so that is where the list stands.

As for top posting, that is an old school habit. Top posting is indeed effective for many people. This message can be clear with historical references down below. It isn't imperative as the text I am adding to this thread, which you are reading now. So, it is secondary to my reply. It IS included below, but it isn't necessary as this message is directed towards yourself. You know what you put before, and it's in your head. If your theory was correct, then every message would have to be quoted in every post. If not, then your email application should have some way of organizing individual messages in order for you to follow the long presentation. A problem with quoting and inserting intermixed is that it gets ugly. A problem with bottom posting is the need to scroll every message that comes into view.

My point is that people have different preferences, and the bottom posting preference only belongs to some. Please don't expect everybody to follow your preference. It is not carved in stone, and can be managed in the preferences of most major email applications for that reason.

Because as an example I have Evaluation 1 - Question 1 - Question 2

If I edit question 2 and change the number it will allow me to do that and it will show this Evaluation 1 - Question 1 - Question 1

And that's what I want to avoid

I tried validates_uniqueness_of :qnumber, :scope => :evaluation_id, :on => :create and it works, but when I edit, I can do what I mention

Hi Colin. I asked about this before in this list. There are no preferences in this list, so that is where the list stands.

As for top posting, that is an old school habit. Top posting is indeed effective for many people. This message can be clear with historical references down below. It isn't imperative as the text I am adding to this thread, which you are reading now. So, it is secondary to my reply. It IS included below, but it isn't necessary as this message is directed towards yourself. You know what you put before, and it's in your head. If your theory was correct, then every message would have to be quoted in every post. If not, then your email application should have some way of organizing individual messages in order for you to follow the long presentation. A problem with quoting and inserting intermixed is that it gets ugly. A problem with bottom posting is the need to scroll every message that comes into view.

My point is that people have different preferences, and the bottom posting preference only belongs to some. Please don't expect everybody to follow your preference. It is not carved in stone, and can be managed in the preferences of most major email applications for that reason.

I'm sorry if I'm doing something wrong, I'm new in this group and I don't want to bother anyone, I'm just hitting the reply button and that's all.

I don't expect everybody to follow my preferences, but if I am spending my time helping someone then I feel at liberty to ask them to post in a manner that makes it easier for me to follow what is going on.

Colin

re-ordering for top -> bottom

Many users, particularly on technical lists, prefer that a reply is inserted at appropriate points into the previous message, so that the full post reads like a question and answer sequence. This requires you to hit reply then scroll through the previous message inserting bits of text at the appropriate points. Whilst doing this you may remove any redundant bits of the previous message. It is not a rule (at least on this list) but many prefer it that way.

Colin

I'm showing evaluation as it shows on my view

EVALUATION

Question Content Created by Number

1 (..........) (..........) Edit 2 (..........) (..........) Edit 3 (..........) (..........) Edit

In my form question

Question number (_______________________________) Content (_______________________________) Created by (_______________________________)

                                              Save

And for example what If I edit "3" and I change it to "2", it shouldn't let someone do that

still not entirely clear but I would think that you need to change...

validates_uniqueness_of :qnumber, :scope => :evaluation_id, :on => :create

to this...

validates_uniqueness_of :qnumber

I'm doing that because I can create several evaluations and each evaluation has an id, that's why I'm scoping by evaluation_id I found in section 15 of the validation guide, something about Proc.new What I want to do with update is that when I hit save it first search if the number exists(with a where() I guess), if exist it shouldn't allow me to update if not, it will let me update the question Do you suggest to use Proc.new?

Thanks

You are still being too vague about your requirement, for me anyway. Can you write the validation test as pseudo code, being absolutely clear about each reference there? Ignore everything about the meaning of the database fields and just write the validation requirement in terms of data in the database and the new data about to be saved, referencing just database fields.

Colin

to JavierQQ <jquarites@gmail.com>:

You are still being too vague about your requirement, for me anyway.

I think I grok what he wants and can express it briefly: the *combination* of evaluation_id and qnumber, must be unique.

If that's right, then the Rails Guide on validation (and callbacks) has a close example:

    validates :name, :uniqueness => { :scope => :year,         :message => "should happen once per year" }

but the OP says that:

         validates_uniqueness_of :qnumber, :scope => :evaluation_id

isn't working, and IIUC should be equivalent to

         validates :qnumber, :uniqueness => { :scope => :evaluation_id }

Just in case I don't UC:

JavierQQ, try this:

    validates :qnumber, :uniqueness => { :scope => :evaluation_id,         :message => "must be unique within each evaluation" }

in the model, and let us know what happens. Don't worry about what controller it's going through to get there.

-Dave

PS: As for top vs. bottom posting, another point of list etiquette that some people have been violating is that if you change the subject, change the Subject. :stuck_out_tongue:

Actually, it doesn't. The topic is old and argued in very many places.

http://www.google.com/search?q=top+posting+vs+bottom+posting

Arguing how people should post online is futile and simply an expression of someone's preferences. That's it. It can be argued into the ground like it has in the past. Unless a list owner demands it, it's a topic that will never be solved.

My preference is top posting because when I follow a thread, I don't have to start over every time I come back to the thread. While I don't really care myself, it's fine by me that people can post above or below. My mail application can segregate quotations just fine, and I don't have any problem getting past it. I don't even notice if it's top or bottom. I personally don't like having to scroll down several pages to see someone else say "ya I've experienced that result as well". It's a waste of time, and my right hand isn't getting any younger every time I use that wheel.