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

On 4 November 2011 15:40, JavierQQ [email protected] wrote:

    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.

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?

On 4 November 2011 16:05, JavierQQ [email protected] wrote:

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

On Nov 4, 4:05pm, JavierQQ [email protected] wrote:

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

On 4 November 2011 16:25, JavierQQ [email protected] wrote:

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

To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

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.

On Nov 4, 2011, at 9:44 AM, BeeRich wrote:

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.


it seems pretty clear that the logical flow of top to bottom favors
bottom posting for anything beyond a short reply that in essence
terminates the discussion.

While I am reasonably neutral on the topic myself - the issue really
becomes one for those who are taking the time to reply and on a topic
that has several replies, the top postings tend to confuse who said
what/when. You should keep in mind that those who reply - especially
those who are knowledgeable such as Frederick and Colin, I would want to
do anything that makes it easy for them to engage/stay engaged but hey,
that’s just me.

Craig

On 4 November 2011 16:44, BeeRich [email protected] wrote:

[snip]

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.

Colin

re-ordering for top -> bottom

On Nov 4, 2011, at 9:55 AM, JavierQQ wrote:

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

  • 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


what is the field/column that is ‘Question 1’? Is it one of the fields
that you only validate on ‘create’? You should probably remove ‘, :on =>
:create’ because that won’t protect uniqueness on ‘update’

Craig

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.

On 4 November 2011 16:57, JavierQQ [email protected] wrote:

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.

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

On 4 nov, 12:11, Craig W. [email protected] wrote:

  • 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


what is the field/column that is ‘Question 1’? Is it one of the fields that you
only validate on ‘create’? You should probably remove ‘, :on => :create’ because
that won’t protect uniqueness on ‘update’

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

On 4 nov, 12:31, Craig W. [email protected] wrote:

On Nov 4, 2011, at 10:20 AM, JavierQQ wrote:

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

On 4 November 2011 17:46, JavierQQ [email protected] wrote:

if not, it will let me update the question
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

On Nov 4, 2011, at 10:20 AM, JavierQQ wrote:

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


Craig W. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[email protected]
1.800.869.6908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
www.ttiassessments.com

Need help communicating between generations at work to achieve your
desired success? Let us help!

On Fri, Nov 4, 2011 at 16:27, Colin L. [email protected] wrote
to JavierQQ [email protected]:

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:


LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern
languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote
work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com
(excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble!
(Aronson)

OKbut then you would also expect other people to expect you to keep a
log of how people’s preferences should be, with you responding
accordingly? That’s unreasonable.