Forum: Ruby on Rails has_one, the = operator and validates_associated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Harm Aarts (Guest)
on 2005-12-20 00:28
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,
http://api.rubyonrails.com/classes/ActiveRecord/As...).
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
Alastair M. (Guest)
on 2005-12-20 03:50
(Received via mailing list)
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

<h2><%= link_to post.title, :action => 'show', :id => post.title %></h2>
<h4>Posted on <%= post.created_on.strftime("%d %b %Y @ %H:%M") %> by
Alastair</h4>
<div><%= post.body %></div>
<p>
<span id="tags"><% for tag in post.tags.split(" ") %> <%= link_to tag,
:action => 'find', :id => tag %><% end %> tags</span>|
<span id="tags"><%= link_to post.comments(:refresh).size, :action =>
'show', :id => post.title, :anchor => "comments" %> comments</span>
</p>

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

<h2><%= link_to post.title, :action => 'show', :id => post.title %></h2>

otherwise

<h2><%= post.title %></h2>

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

Thanks in advance,

Alastair
Alastair M. (Guest)
on 2005-12-20 04:23
(Received via mailing list)
Alastair M. wrote:
> <div><%= post.body %></div>
> <h2><%= link_to post.title, :action => 'show', :id => post.title %></h2>
>
> otherwise
>
> <h2><%= post.title %></h2>
>
> 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
Mark Reginald J. (Guest)
on 2005-12-20 04:41
(Received via mailing list)
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.
Alastair M. (Guest)
on 2005-12-20 12:47
(Received via mailing list)
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
This topic is locked and can not be replied to.