Forum: Ruby on Rails a simple problem but difficult

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.
x-1101 (Guest)
on 2006-04-29 03:35
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
Jodi S. (Guest)
on 2006-04-29 04:01
(Received via mailing list)
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
Reynaldo (Guest)
on 2006-04-29 04:17
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'
Jodi S. (Guest)
on 2006-04-29 04:37
(Received via mailing list)
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
Reynaldo (Guest)
on 2006-04-29 04:49
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' %>

<!--[form:catelemento]-->
userid <%= session[:user_id] %>

<p><label for="catelemento_Comandancia">Comandancia</label><br/>
<select id="catelemento_Comandancia" name="catelemento[Comandancia]">
<%= options_from_collection_for_select @komandancias, "id", "Nombre" %>
  </select>
Jodi S. (Guest)
on 2006-04-29 05:20
(Received via mailing list)
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.
Reynaldo (Guest)
on 2006-04-29 05:35
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
Jodi S. (Guest)
on 2006-04-29 05:47
(Received via mailing list)
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
This topic is locked and can not be replied to.