0

我不知道如何从这个数组中提取特定的项目。这是数组。

2.1.3 :091 > User.transactions
 => [["0AZ0De04KqsreDgVwM1RSRYjyd8yXxSDQ8Zxn","XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", 200, "ATM Withdrawal",{"location"=>{"city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"city"=>1, "state"=>1}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Withdrawal", "ATM"], "21012002"], ["3mg4qV4JZycjewvKEzrLTYMzdr1MmvcO4Z3zX", "XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", 240, "Online Transfer from External Sav ...3092", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["KdDjmojBERUKx3JkDdO5IaRJdZeZKNuK4bnKJ1", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 2307.15, "Apple Store", {"location"=>{"address"=>"1 Stockton St", "city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Shops", "Computers and Electronics"], "19013000"], ["DAE3Yo3wXgskjXV1JqBDIrDBVvjMLDCQ4rMQdR", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 3.19, "Gregorys Coffee", {"location"=>{"address"=>"874 Avenue of the Americas", "city"=>"New York", "state"=>"NY"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["1vAj1Eja5BIn4R7V6Mp1hBPQgkryZRHryZ0rDY", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 80, "ATM Withdrawal", {"location"=>{"city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"city"=>1, "state"=>1}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Withdrawal", "ATM"], "21012002"], ["zq7MLAM4N3cjeKvXP9YqtBJXvZeajJCkjQakYv", "QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", -240, "Online Transfer from Chk ...1702", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["96d5AO5gLjC9EowVyn5OCBRjJR9LaOHJnBVJzd", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 240, "Online Transfer to Sav ...9606", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["VK0EQ5Ea13u9Qwzm6nA8CNaze8gdJoCJvx6JDO", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", -0.93, "Interest Payment", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>0.2}, {"primary"=>"unresolved"}, ["Interest"], "15000000"], ["aJPEm5EVqxF6yk8K5nPeFbDpnPR57wI3xMR3pP", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 12.74, "Golden Crepes", {"location"=>{"address"=>"262 W 15th St", "city"=>"New York", "coordinates"=>{"lat"=>40.740352, "lon"=>-74.001761}, "state"=>"NY"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, nil, nil], ["moPE4dE1yMHJX5pmRzwrcvpQqPdDnZHEKPREYL", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 7.23, "Krankies Coffee", {"location"=>{"address"=>"211 E 3rd St", "city"=>"Winston Salem", "state"=>"NC"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["P1xJboJA5Ls31gJrMQEBU5dZD3NmPrH5R6g5jL", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 118.23, "Banana Republic", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>0.2}, {"primary"=>"digital"}, ["Shops", "Digital Purchase"], "19019000"], ["dRBp95pEwZfMXENvpw3YHY43VNK4LVSP7RkPpA", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", -800, "Venmo Cashout 18375552", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Third Party", "Venmo"], "21010001"], ["moPE4dE1yMHJX5pmRzwrcvpkxdopLxtEKPREYo", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 120, "Venmo Payment 16991172", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Third Party", "Venmo"], "21010001"], ["JmN0JX0q5EcaQJM9ZbOwUYyyp607m4u3PR63Vn", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 5.32, "Octane Coffee Bar and Lounge", {"location"=>{"address"=>"1009 Marietta St Nw # B", "city"=>"Atlanta", "state"=>"GA"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["4r0aBVa85Kt3BDPk10a4U5OD3XKjE7Hzxpez6B", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 28.57, "Papa Johns Pizza", {"location"=>{"address"=>"2625 David Dr", "city"=>"Metairie", "coordinates"=>{"lat"=>29.999986, "lon"=>-90.21869}, "state"=>"LA", "zip"=>"70003"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1, "zip"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Pizza"], "13005012"], ["eJXpMzpR65FP4RYno6yjhBDxgkM6pXC9RKM9o3", "QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", -3042.44, "Company Payroll", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Payroll"], "21009000"], ["0AZ0De04KqsreDgVwM1RSRYjyd8yXxSDQ8Zxn", "XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", 200, "ATM Withdrawal", {"location"=>{"city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"city"=>1, "state"=>1}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Withdrawal", "ATM"], "21012002"], ["3mg4qV4JZycjewvKEzrLTYMzdr1MmvcO4Z3zX", "XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", 240, "Online Transfer from External Sav ...3092", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["KdDjmojBERUKx3JkDdO5IaRJdZeZKNuK4bnKJ1", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 2307.15, "Apple Store", {"location"=>{"address"=>"1 Stockton St", "city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Shops", "Computers and Electronics"], "19013000"], ["DAE3Yo3wXgskjXV1JqBDIrDBVvjMLDCQ4rMQdR", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 3.19, "Gregorys Coffee", {"location"=>{"address"=>"874 Avenue of the Americas", "city"=>"New York", "state"=>"NY"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["1vAj1Eja5BIn4R7V6Mp1hBPQgkryZRHryZ0rDY", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 80, "ATM Withdrawal", {"location"=>{"city"=>"San Francisco", "state"=>"CA"}}, nil, false, {"location"=>{"city"=>1, "state"=>1}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Withdrawal", "ATM"], "21012002"], ["zq7MLAM4N3cjeKvXP9YqtBJXvZeajJCkjQakYv", "QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", -240, "Online Transfer from Chk ...1702", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["96d5AO5gLjC9EowVyn5OCBRjJR9LaOHJnBVJzd", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 240, "Online Transfer to Sav ...9606", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Account Transfer"], "21001000"], ["VK0EQ5Ea13u9Qwzm6nA8CNaze8gdJoCJvx6JDO", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", -0.93, "Interest Payment", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>0.2}, {"primary"=>"unresolved"}, ["Interest"], "15000000"], ["aJPEm5EVqxF6yk8K5nPeFbDpnPR57wI3xMR3pP", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 12.74, "Golden Crepes", {"location"=>{"address"=>"262 W 15th St", "city"=>"New York", "coordinates"=>{"lat"=>40.740352, "lon"=>-74.001761}, "state"=>"NY"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, nil, nil], ["moPE4dE1yMHJX5pmRzwrcvpQqPdDnZHEKPREYL", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 7.23, "Krankies Coffee", {"location"=>{"address"=>"211 E 3rd St", "city"=>"Winston Salem", "state"=>"NC"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["P1xJboJA5Ls31gJrMQEBU5dZD3NmPrH5R6g5jL", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 118.23, "Banana Republic", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>0.2}, {"primary"=>"digital"}, ["Shops", "Digital Purchase"], "19019000"], ["dRBp95pEwZfMXENvpw3YHY43VNK4LVSP7RkPpA", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", -800, "Venmo Cashout 18375552", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Third Party", "Venmo"], "21010001"], ["moPE4dE1yMHJX5pmRzwrcvpkxdopLxtEKPREYo", "nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", 120, "Venmo Payment 16991172", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Third Party", "Venmo"], "21010001"], ["JmN0JX0q5EcaQJM9ZbOwUYyyp607m4u3PR63Vn", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 5.32, "Octane Coffee Bar and Lounge", {"location"=>{"address"=>"1009 Marietta St Nw # B", "city"=>"Atlanta", "state"=>"GA"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Coffee Shop"], "13005043"], ["4r0aBVa85Kt3BDPk10a4U5OD3XKjE7Hzxpez6B", "pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", 28.57, "Papa Johns Pizza", {"location"=>{"address"=>"2625 David Dr", "city"=>"Metairie", "coordinates"=>{"lat"=>29.999986, "lon"=>-90.21869}, "state"=>"LA", "zip"=>"70003"}}, nil, false, {"location"=>{"address"=>1, "city"=>1, "state"=>1, "zip"=>1}, "name"=>0.2}, {"primary"=>"place"}, ["Food and Drink", "Restaurants", "Pizza"], "13005012"], ["eJXpMzpR65FP4RYno6yjhBDxgkM6pXC9RKM9o3", "QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", -3042.44, "Company Payroll", {"location"=>{}}, nil, false, {"location"=>{}, "name"=>1}, {"primary"=>"special"}, ["Transfer", "Payroll"], "21009000"]]

我在rails控制台中试过这个:

user.transactions.select { |t| t.category == "21009000" } 

我得到了回应:

NoMethodError: undefined method `category' for #<Array:0xc18e26c>
4

1 回答 1

2

你有一个数组数组,所以没有属性category

如果您需要找到包含的数组'21009000',您可以使用user.transactions.select{|attributes| attributes.include? '21009000'}

于 2015-04-10T20:52:40.287 回答