Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:05:01+00:00 2026-06-10T16:05:01+00:00

I need to create a method which can be called so list_of_hash_values = MyModel.values_and_links

  • 0

I need to create a method which can be called so

list_of_hash_values = MyModel.values_and_links :title, "some_query_param", lambda{|id| url_for :controller => :my_model, :action => :show, :id=> id }

It gets 3 arguments: field name (as a symbol), query parameter and lambda with one parameter. And it returns hash of values and their urls. Lambda return some string value (in this case it is ulr of a model instance).

I tried to do:

class MyModel

 def values_and_links(title, query_param, lambda_exp)

   instance_values = MyModel.order(:title).where("#{title} = ?", "%#{query_param}%")
   instance_values.each do |item|
      {:hash_key1 => item.send(title), :hash_key2 => lambda_exp.call(title)} 
   end

 end
end

It’s wrong, not working.

I hope you understand what I need to accomplish. How can I do it? How can I implement method values_and_links?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:05:03+00:00Added an answer on June 10, 2026 at 4:05 pm

    I would use a block instead:

    class MyModel
     def values_and_links(title, query_param)
       instance_values = MyModel.order(:title).where("#{title} = ?", "%#{query_param}%")
       instance_values.each do |item|
          {:hash_key1 => item.send(title), :hash_key2 => yield(title)} 
       end
     end
    end
    

    then, you can call your method like this:

    list_of_hash_values = MyModel.values_and_links :title, "some_query_param" do |id|
      url_for :controller => :my_model, :action => :show, :id=> id
    end
    

    or, for ‘short’:

    list_of_hash_values = MyModel.values_and_links :title, "some_query_param" {|id| url_for :controller => :my_model, :action => :show, :id=> id}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to execute a method which can only be called once my
I need to create a method to generate a unit vector in three dimensions
I need to create an Objective-C method that converts an int into a byte
This is a design question: when do I need to create/use a static method
I need some JS class or CSS method to create from any text such
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
well i need to make a class called TList which have a private attribute
I am trying to create an extension method that i can forward a IList
I created an HtmlHelper method called DisplayListBoxFor that will need display a textual representation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.