SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Swapping key names in a hash

    Hello,

    I'm trying to change the name of a hash key. I want to take a params[:value] and modify some of it's key names to work better in model sql statements.

    for instance I have a param hash key named "title" (params['title']) and I want to change that key name to 'spaces.title' so it will work better w/in a conditional statement in joined table sql call.

    Any ideas?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I'm not even sure you can alter the params hash. I've never tried it personally.

    In any case, I'd just create a new hash with the key names you want, then pass that to whatever SQL-creating method you need.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't do this ;-).

    Could you describe your query, params hash and database tables. I'm sure there is a Rails-way to handle this.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting column names from a record set

    Hello,

    I'm trying to get the column names associated w/ this record set:


    account_all = Account.findall)


    Since account_all is an array - how do I get the names?


    I know there's got to be a simple way to do this.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course there is!

    Code:
    Account.columns  # returns all columns
    Account.content_columns  # returns all columns you want to show to your user (excludes id, xxx_id, xxx_count columns)
    This will display all columns along with the values in the @account object:

    Code:
    <dl>
    <&#37; for column in Account.content_columns %>
    <dt><%= column.human_name %></dt>
    <dd><%= @account.send(column) %></dd>
    <% end %>
    </dl>
    I'm not 100% sure that @account.send(column) works though.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet - here's what I was looking for:

    account_all.first.attributes.keys

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want *that* you can use Account.column_names.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •