简体   繁体   中英

Rails 3.1 and Mysql Position in table

Using Rails 3.1 and mysql,I've the following issue: I've created the array @addresses:

@registry_all = Registry.all 
@addresses = @registry_all.map{|reg| [ reg.id, reg.indirizzo_1, reg.indirizzo_2, reg.indirizzo_3, reg.indirizzo_4 ]}.sort

and this is model relative to Registry:

rails g model registry id:integer, created_at:datetime, updated_at:datetime, name:string, cod_fiscale:string :limit=>16, p_iva:string :limit=>11, indirizzo_1:text, indirizzo_2:text, indirizzo_3:text, indirizzo_4:text ,telefono1:string :limit=>25, telefono2:string :limit=>25

So array @addresses contains all registry's addresses and now I want to retrieve all addresses owned by a singular registry, making a finding by registry.id. I mean,I was thinking something similar to that:

@addresses.find[registry.id]

but off course doing so, (supposing that registry.id has equals to 30), I retrieve all addresses owned by 30-th element of array @addresses and not addresses owned by registry.id. This should works only if object for what I'm looking for, is 30-th element of array addresses, in other words, only if this object is 30-th inside Registry mysql table How can I do this?

If I understood well you have all the 4 addresses [1] for a registry in a row of the registries DB table with the names indirizzo_1, indirizzo_2, indirizzo_3, indirizzo_4 and you want these addresses in an array. You could do this:

registry = Registry.find(the_registry_id)
return [registry.indirizzo_1, registry.indirizzo_2, registry.indirizzo_3, registry.indirizzo_4]

With find you get the Registry with that ID and then you use return the 4 attributes as an array.

But from a design point of view I would not add the 4 fields for the addresses but I would use a one-to-many relationship since a registry can have many addresses.

[1] "indirizzo" is "address" in italian

I created a migration somewhat like yours (minus the :limit)

    class CreateRegistries < ActiveRecord::Migration
      def change
        create_table :registries do |t|
          t.integer :id
          t.datetime :created_at
          t.datetime :updated_at
          t.string :name
          t.string :cod_fiscale
          t.string :p_iva
          t.text :indirizzo_1
          t.text :indirizzo_2
          t.text :indirizzo_3
          t.text :indirizzo_4
          t.string :telefono1
          t.string :telefono2

          t.timestamps
        end
      end
    end

I assume you have already done:

$ bundle exec rake db:migrate

A simple way to think of this is to open a rails console:

rails c

I created 5 Registry records like this:

Registry.create(name:"name5", cod_fiscale:"cod_fiscale", p_iva:"p_iva", indirizzo_1:"txt", indirizzo_2:"txt", indirizzo_3:"txt", indirizzo_4:"txt" ,telefono1:"12345")

So now typing:

Registry.count

Gives me:

1.9.2-p180 :017 > Registry.count
(0.2ms)  SELECT COUNT(*) FROM "registries" 
=> 6 

You can now use the .find method like this:

Registry.find(1)

where (1) is the id of one of the 6 records we have.

If using this in a Controller it will look like this:

@registry = Registry.find(params[:id])

Then you could grab the indirizzo by typing:

Registry.find(1).indirizzo_1

which gives:

1.9.2-p180 :036 > Registry.find(1).indirizzo_1
Registry Load (0.3ms)  SELECT "registries".* FROM "registries"  WHERE "registries"."id" = ? LIMIT 1  [["id", 1]]
=> "txt" 

Let me know if you need more help and feel free to include more output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM