0

How do I create an array from database columns?

My table Reklamer:

id  virksomhed  dato                   klik     
10  Iqmedier    2011-02-15 00:00:00     4   
11  Iqmedier    2011-02-15 00:00:00     2   
12  Iqmedier    2011-02-15 00:00:00     3   

I want to create an array of all the columns klik. Like [4, 2, 3]

An can someone explain what a block and an object is :) Or where I can read something about it.

2
  • 2
    @Rails beginner, FYI, on StackOverflow they discourage using greetings (salutations), signatures and thanking people in advance or "best regards" and similar (validictions). Commented Feb 16, 2011 at 21:36
  • And they also discourage having "Simple qu[e]stion" in the question title. Commented Feb 16, 2011 at 22:04

1 Answer 1

1

I'm guessing you have a model named Reklamer too.

You can do the following to get the array

Reklamer.select('klik').all.map(&:klik)

Edit:

(Array.map in Ruby) map(&:klik) is just a shorthand for

.map do |record|
  record.klik
end

It returns the value of klik for each record, and creates a new array using those values.

Sign up to request clarification or add additional context in comments.

3 Comments

What does .map(&:klik)? Does it create an array?
How do i only create an array of those were the column Virksomhed = Iqmedier. I have tried with this data: <%= Reklamer.select(:all, :conditions => {:virksomhed => 'Iqmedier'}).select('earn').all.map(&:earn) %> But gets a error: wrong number of arguments (2 for 1)
Try Reklamer.where(:virksomhed => 'Iqmedier').all.map(&:earn)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.