A simple problem but difficult


#1

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


#2

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


#3

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:innew’


#4

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


#5

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" %>


#6

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


#7

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.


#8

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