# Has\_one, the = operator and validates\_associated

**URL:** <https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683>\
**Category:** Rails\
**Created:** [December 19, 2005, 11:28pm UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683 "2005-12-19T23:28:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://www.ruby-forum.com/u/Harm_Aarts)\
**Post date:** [December 19, 2005, 11:28pm UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683/1 "2005-12-19T23:28:55Z")

</div>

Dear readers,

I am puzzles by the following, imagine models A and B. A has\_one B and B  
belongs\_to A. So far so good. But rails does this great thing by  
relfecting on this relationship and enables me use the equals sign to do  
A.B=B. But now comes the puzzlement. Whenever you do this B is supposed  
to be saved (see,  
[Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000472)).  
Now a couple of questions, where do the errors go if one implemented a  
validate method? I guess nowhere and they are ignored. Moreover if you  
use ‘validates\_associated :B’, which makes sense, you are forced to use  
the equals sign method, which supposedly already saves the model (which  
it actually doesn’t, I checked). So when is a instantiation of a model  
really saved?

With kind regards,  
Harm

---

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://www.ruby-forum.com/u/Harm_Aarts)\
**Post date:** [December 20, 2005, 2:50am UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683/2 "2005-12-20T02:50:07Z")

</div>

Hello all,

Excuse me if I’ve got my terms incorrect (please correct me if so!).

I’ve created a partial for this blog application I’m writing, which  
contains the following

## \<%= link\_to post.title, :action =\> 'show', :id =\> post.title %\>

#### Posted on \<%= post.created\_on.strftime("%d %b %Y @ %H:%M") %\> by Alastair

\<%= post.body %\>

\<% for tag in post.tags.split(" ") %\> \<%= link\_to tag, :action =\> 'find', :id =\> tag %\>\<% end %\> tags| \<%= link\_to post.comments(:refresh).size, :action =\> 'show', :id =\> post.title, :anchor =\> "comments" %\> comments

What I would like to do is depending on which action was called, write  
out different code. So if the action was list or index, then write out

## \<%= link\_to post.title, :action =\> 'show', :id =\> post.title %\>

otherwise

## \<%= post.title %\>

Simple but not knowing the ruby/rails terms, I’m struggling to find the  
right search term in google!

Thanks in advance,

Alastair

---

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://www.ruby-forum.com/u/Harm_Aarts)\
**Post date:** [December 20, 2005, 3:23am UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683/3 "2005-12-20T03:23:10Z")

</div>

Alastair M. wrote:

> \<%= post.body %\>
> 
> ## \<%= link\_to post.title, :action =\> 'show', :id =\> post.title %\>
> 
> otherwise
> 
> ## \<%= post.title %\>
> 
> Simple but not knowing the ruby/rails terms, I’m struggling to find the  
> right search term in google!

Ok, what I’ve since found out is using the render (:layout/:partial)  
within the method. So I’m going to tinker around with that and see what  
I come up with!

Alastair

---

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://www.ruby-forum.com/u/Harm_Aarts)\
**Post date:** [December 20, 2005, 3:41am UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683/4 "2005-12-20T03:41:11Z")

</div>

Alastair M. wrote:

> What I would like to do is depending on which action was called, write  
> out different code.

The name of the action is available in the view as params[:action]

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Harm\_Aarts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a5b964/32.png) [@Harm\_Aarts](https://www.ruby-forum.com/u/Harm_Aarts)\
**Post date:** [December 20, 2005, 11:47am UTC](https://www.ruby-forum.com/t/has-one-the-operator-and-validates-associated/50683/5 "2005-12-20T11:47:57Z")

</div>

Mark Reginald J. wrote:

> Alastair M. wrote:
> 
> > What I would like to do is depending on which action was called, write  
> > out different code.
> 
> The name of the action is available in the view as params[:action]

Ah thank you! even better
