# Newbie: How to refer to 'this' instance's items

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588>\
**Category:** Rails\
**Created:** [August 21, 2007, 5:38am UTC](https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588 "2007-08-21T05:38:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 21, 2007, 5:38am UTC](https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588/1 "2007-08-21T05:38:30Z")

</div>

Hi,

I’ve been playing with Rails for a few months now and I enjoying it. But  
most recently, I’ve just come across an issue that I just can’t seem to  
resolve, to the point where I’ve recreated a small application from  
scratch to reproduce the issue (and I’m confident the problem is with me  
not knowing the language well enough).

Here’s the issue…

I have ACCOUNTS that hold zero or more OPERATIONS. Each OPERATION has a  
FROM ACCOUNT and a TO ACCOUNT (think accounting). See below for some  
schema information.

Here’s the code for the account.rb model:

class Account \< ActiveRecord::Base  
has\_many :operations, :order =\> “actual\_date”  
def has\_operations?  
#  
# The problem is the “???” what do I replace them with?  
# :id doesn’t work  
# @id doesn’t work wither  
# self.id doesn’t work  
#  
if ( Operation.find\_by\_from\_account\_id( ??? ).size \> 0 ||  
Operation.find\_by\_to\_account\_id( ??? ).size \> 0 )  
return true  
else  
return false  
end  
end  
end

And here’s my show.rhtml

\<% for column in Account.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @account.send(column.name) %\>

\<% end %\>

\<% if @account.has\_operations? %\>

### Operations
 \<% for operation in @account.expenses %\> \<% end %\> 

| \<%= operation.actual\_date %\> | \<%= link\_to 'Show', :action =\> 'show', :id =\> operation %\> | \<%= link\_to 'Edit', :action =\> 'edit', :id =\> operation %\> | \<%= link\_to 'Destroy', { :action =\> 'destroy', :id =\> operation }, :confirm =\> 'Are you sure?', :method =\> :post %\> |

\<% end %\> \<%= link\_to 'Edit this account', :action =\> 'edit', :id =\> @account %\> | \<%= link\_to 'Return to list', :action =\> 'list' %\>

Now the above works if I remove the “has\_operations?” method from my  
“account.rb” file AND my Operations table looks like this:

create\_table “operations”, :force =\> true do |t|  
t.column “actual\_date”, :datetime  
t.column “amount”, :decimal  
t.column “account\_id”, :integer  
t.column “to\_recipient”, :string  
end

\*Notice the “account\_id” instead of “to\_account\_id” and  
“from\_account\_id”.

As Rails is nice enough to understand the “has\_many” and “belongs\_to”  
relationships. But when I change “account\_id” to “from\_account\_id” and  
“to\_account\_id”;

create\_table “operations”, :force =\> true do |t|  
t.column “actual\_date”, :datetime  
t.column “amount”, :decimal  
t.column “from\_account\_id”, :integer  
t.column “to\_account\_id”, :integer  
end

the dynamically generated “has\_operations?” method shits to bed as it  
cannot find “account\_id” (makes sense). So I replace it (maybe there’s  
another way) with the version shown above. This new method gets called  
directly from the “show.rhtml” file and all I need from the instance is  
the “id” – which Rails discovers and uses in it’s own “has\_operations?”  
method when it generates it, but I can’t seem to get to with my  
version…

Your help would be much appreciated!

Thanks!

Marc :o)

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 21, 2007, 5:53am UTC](https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588/2 "2007-08-21T05:53:06Z")

</div>

On 8/21/07, Marc B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s the issue…  
> #  
> end
> 
> %\>
> 
> As Rails is nice enough to understand the “has\_many” and “belongs\_to”  
> the dynamically generated “has\_operations?” method shits to bed as it
> 
> ## Marc :o)

Marc

I believe you’re finding it difficult because your associations aren’t  
quite  
setup right.

If you want to use from\_account\_id and to\_account\_it in operations, this  
suggests two assocaitions

You might model it something like this.

class Account \< ActiveRecord::Base

has\_many :operations\_from, :class\_name =\> “Operations”, :foreign\_key  
=\>  
“from\_account\_id”, :order =\> “actual\_date”  
has\_many :operations\_to, :class\_name =\> “Operations”, :foreign\_key =\>  
“to\_account\_id”, :order =\> “actual\_date”

def has\_operations?  
operations\_from.count \> 0 || operations\_to.count \> 0  
end  
end

When you’re inside the account model you shouldn;t use Operation.find  
class  
methods much if at all. You should use the assocation methods setup by  
the  
has\_many and belongs to.

You should use this version of your migrations for this.

create\_table “operations”, :force =\> true do |t|  
t.column “actual\_date”, :datetime  
t.column “amount”, :decimal  
t.column “from\_account\_id”, :integer  
t.column “to\_account\_id”, :integer  
end

You should setup the belongs\_to call similarly to the has\_many

HTH  
Daniel

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 21, 2007, 6:22am UTC](https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588/3 "2007-08-21T06:22:02Z")

</div>

Daniel ----- wrote:

> On 8/21/07, Marc B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Here’s the issue…  
> > #  
> > end
> > 
> > %\>
> > 
> > As Rails is nice enough to understand the “has\_many” and “belongs\_to”  
> > the dynamically generated “has\_operations?” method shits to bed as it
> > 
> > ## Marc :o)
> 
> Marc
> 
> I believe you’re finding it difficult because your associations aren’t  
> quite  
> setup right.
> 
> If you want to use from\_account\_id and to\_account\_it in operations, this  
> suggests two assocaitions
> 
> You might model it something like this.
> 
> class Account \< ActiveRecord::Base
> 
> has\_many :operations\_from, :class\_name =\> “Operations”, :foreign\_key  
> =\>  
> “from\_account\_id”, :order =\> “actual\_date”  
> has\_many :operations\_to, :class\_name =\> “Operations”, :foreign\_key =\>  
> “to\_account\_id”, :order =\> “actual\_date”
> 
> def has\_operations?  
> operations\_from.count \> 0 || operations\_to.count \> 0  
> end  
> end
> 
> When you’re inside the account model you shouldn;t use Operation.find  
> class  
> methods much if at all. You should use the assocation methods setup by  
> the  
> has\_many and belongs to.
> 
> You should use this version of your migrations for this.
> 
> create\_table “operations”, :force =\> true do |t|  
> t.column “actual\_date”, :datetime  
> t.column “amount”, :decimal  
> t.column “from\_account\_id”, :integer  
> t.column “to\_account\_id”, :integer  
> end
> 
> You should setup the belongs\_to call similarly to the has\_many
> 
> HTH  
> Daniel

Thanks so much Daniel! :o)

My Rails inexperience shows. I coded for 15 years (C, C++, Java) and  
after a 6 year break – leading and managing J2EE development teams –  
I’ve rediscovered programming through Ruby on Rails and I’m having a  
blast (too bad I can only spend 3-4 hours a week on this).

Have a good one!

Marc

---

<div class="post-metadata">

**Author:** ![marcbey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@marcbey](https://www.ruby-forum.com/u/marcbey)\
**Post date:** [August 21, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/newbie-how-to-refer-to-this-instances-items/112588/4 "2007-08-21T19:28:26Z")

</div>

On 8/21/07, Marc B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > As Rails is nice enough to understand the “has\_many” and “belongs\_to”  
> > > quite  
> > > has\_many :operations\_from, :class\_name =\> “Operations”, :foreign\_key  
> > > When you’re inside the account model you shouldn;t use Operation.find  
> > > t.column “from\_account\_id”, :integer
> 
> My Rails inexperience shows. I coded for 15 years (C, C++, Java) and  
> after a 6 year break – leading and managing J2EE development teams –  
> I’ve rediscovered programming through Ruby on Rails and I’m having a  
> blast (too bad I can only spend 3-4 hours a week on this).
> 
> Have a good one!
> 
> ## Marc

Yeah Ruby’s great. I really like it but I don’t get enough time on it  
either 😉

Glad I could help.

-Daniel
