0

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'm trying different code.

Does anyone has an idea? Thanks!

2 Answers 2

1

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

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

Comments

0

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)

5 Comments

Maybe I explained it wrong: 'FeedURI' is the record in the db wich contains an array of uri's 'uri' is the variable with the current uri so I'm trying to read from an array in a record (FeedURI) wich contains the current (uri)
Hmm, that seems a little strange. If the database field is called FeedURI, it is not following rails naming convention... Anyway, I guess you could use sql syntax instead, :conditions => ["FeedURI contains ?", uri] Would that work?
Thanks for the DB naming info (I'll be changing that tonight). 'contains' throws an error, I've tried it with 'like' and '=', they both work but it returns nothing...very strange. I'll have to dive into the SQL syntax some more.
I'm not an expert in sql unfortunately so I just wrote what you wrote in the question :) My guess though is that this kind of syntax might not be common for all databases. But for MySQL, try ["FeedURI.contains(?)", uri]
I solved it with this syntax ["FeedURI like ?", "%#{uri}%"] Thanks for the help

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.