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 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.
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
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?
I'm doing it
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)
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
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.
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 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.
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.
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?
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.
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.
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.