# A simple problem but difficult

**URL:** <https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144>\
**Category:** Rails\
**Created:** [April 29, 2006, 1:35am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144 "2006-04-29T01:35:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 1:35am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/1 "2006-04-29T01:35:08Z")

</div>

how can i access to a collection that was created in the same control  
and the data that a want to access isnÂ´t the id

the collection has : id, usuario, comandancia

comandancia is the data to access

the question is how to access comandancia in @acceso

this code is wrong :

def new  
@catelemento = Catelemento.new  
@acceso= Catacceso.find(:all,  
:conditions =\> [“Usuario = ?”,  
session.[:user\_id]])

```
@komandancias = Catcomandancia.find(:all,
                          :conditions => ["id = ?", 

```

@acceso.Comandancia ])

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 2:01am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/2 "2006-04-29T02:01:05Z")

</div>

This is just a stab…

Do you have a column in Cataccesco called “Comandancia” ?Ruby will  
reference it as “comandancia” (lowercase).

If this is this case, then the line would look like this:

@komandancias = Catcomandancia.find(:all, :conditions =\> [“id = ?”,  
@acceso.comandancia ])

Let us know your name next time. grin.

Cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 2:17am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/3 "2006-04-29T02:17:51Z")

</div>

my name is reynaldo

and what you say don’t work

my table is called catAccesos

look

undefined method `comandancia’ for #Catacceso:0x3972a98  
RAILS\_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:1501:in  
`method_missing' #{RAILS_ROOT}/app/controllers/elementos_controller.rb:24:in`new’

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 2:37am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/4 "2006-04-29T02:37:33Z")

</div>

Hey Reynaldo.

hmm.

So your table Catacceso has a column comandancia? Or is Comandancia a  
table as well?

Can you please post your model for Catacceso?

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 2:49am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/5 "2006-04-29T02:49:33Z")

</div>

HI Jodi

tankhs

look this is my proyect

tables:

catComandancias  
id  
clave  
nombre

users  
id  
nombre  
password

cataccesos  
id  
Usuario  
Comandancia

catelementos  
id  
comandancia  
clave  
nombre  
direccion

in my controller elementos the def to the new operation is

def new  
@catelemento = Catelemento.new  
@acceso= Catacceso.find(:first,  
:conditions =\> [“Usuario = ?”,  
session[:user\_id]])

```
@komandancias = Catcomandancia.find(:all,
                                    :conditions => ["Usuario = ?",
                                    @acceso.comandancia])

```

end

in the file \_form.rhtm i want to filter the select with the comandancias  
that have the Usuario in catacceso (just one comandancia)

\_form.rhtml code in the follow:

\<%= error\_messages\_for ‘catelemento’ %\>

userid \<%= session[:user\_id] %\>

Comandancia  
\<%= options\_from\_collection\_for\_select @komandancias, "id", "Nombre" %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 3:35am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/6 "2006-04-29T03:35:37Z")

</div>

Jodi

my models files are void

i donÂ´t change them  
except user.rb

i’m going to resume the problem

1.- i have a controller of elementos

2.- i full the collection @acceso  
@acceso = catAcceso.find(:all)

3.- i want to access to the data of @acceso in the same control

## users.rb

require “digest/sha1”  
class User \< ActiveRecord::Base  
attr\_accessor :password  
attr\_accessible :nombre, :password

validates\_presence\_of :nombre, :password  
validates\_uniqueness\_of :nombre

def self.login(nombre, password)  
passwordA = hash\_password(password || “”)  
find(:first,  
:conditions =\> [“nombre = ? and passwordA = ?”,  
nombre, passwordA])  
end

def try\_to\_login  
User.login(self.nombre, self.password)  
end

def before\_create  
self.passwordA = User.hash\_password(self.password)  
end

before\_destroy :dont\_destroy\_vasc

def dont\_destroy\_vasc  
raise “No se puede eliminar el usuario Administrador” if self.nombre  
== ‘Administrador’  
end

def after\_create  
@password = nil  
end

private  
def self.hash\_password(password)  
Digest::SHA1.hexdigest(password)  
end  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 3:20am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/7 "2006-04-29T03:20:06Z")

</div>

Ok Reynaldo, that helps, but I’m still a little foggy.

The ‘tables’ you provided are table descriptions correct? Do you have  
models as well? Please paste all your models in.

And as a suggestion, you’ll get more predictable behavior from Rails  
if you don’t Camel Case (that is catComandancias) but with  
underscores so that your table would be called ‘cat\_comandancias’.

Finally, I’m still struggling to understand what exactly the problem  
is. I know you’re trying to filter Catcomandancia down to those  
matching one row in Catacceso.

What is happening that you don’t want?

We’ll get this fixed Reynaldo. I wish I spoke your language, then  
we’d have an easier time.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2006, 3:47am UTC](https://www.ruby-forum.com/t/a-simple-problem-but-difficult/59144/8 "2006-04-29T03:47:17Z")

</div>

Reynaldo,

How do you know you can’t “access to the data of @acceso in the same  
control” ?? In order for me to help, I need to understand the  
symptoms you’re seeing.

Maybe someone else can help out. Im not seeing your problem.

good luck!

J
