开发者

Extract data from array in DB (rails)

开发者 https://www.devze.com 2023-02-05 17:28 出处:网络
I am trying to extract some data from an array with the following syntax: @entries_from_db = XrEntry.find(:all, :conditions => [:FeedURI => uri ], :select => \'json\')

I am trying to extract some data from an array with the following syntax:

@entries_from_db = XrEntry.find(:all, :conditions => [:FeedURI => uri ], :select => 'json')

The :FeedURI is the record that contains an array with uri's ["123456", "23345", "4453"] The uri is the variable wich contains the current uri.

The statement I'm trying to make is 'select JSON from XrEntry where FeedURI contains uri' Im stuck on the part to access the array and always get several error msg's when I开发者_StackOverflow社区'm trying different code.

Does anyone has an idea? Thanks!


I solved it with this syntax

@entries_from_db = XrEntry.find(:all, :conditions => ["FeedURI like ?", "%#{uri}%"] , :select => 'json')

the "%#{your_rails_variable}%" is needed to read in an array


You seem to have switched the condition syntax. you chould start with the db attribute and then the variable.

@entries_from_db = XrEntry.find(:all, 
                                :conditions => { :uri => FeedURI }, 
                                :select => 'json')

That will return an array of XrEntry objects with only the json attribute present. To get an array of only the json data you could map it like this:

@json_array = @entries_from_db.map(&:json)
0

精彩评论

暂无评论...
验证码 换一张
取 消