i have two two models post, tags and i want to get post where some tags.
tag.rb
post_id integer 3
tags array {'v1', 'v2'}
when search in tag model work Tag.where('tag && array[['v1', 'v2']]')
but when add this in post not work Post.where('tags.tags && array[['v1', 'v2']]')
every post has one tag
The most direct/raw way to do this would be something like this:
Post.joins(:tags).where("ARRAY['v1', 'v2'] <# tags.tags")
Obligatory warning about injecting user-inputted values into strings sent to a database, for the WHERE clause.
Related
I want to compare a param extracted from a link to the list of data present in column...
I am using pluck to generate a array of list(in controller) but not getting any success in comparing it
any method that I can fetch records in model and compare with param in controller or model
as passing controller instances in model seems inappropriate to me
Initially i am trying fetching and comparing in controller..
#abc=Group.pluck(:group_token)
what I tried to do before is defined group_fetch method in model and used it in controller to check condition but I was not able to compare param which comes frome url dynamically
def self.group_fetch
Group.find_by group_token: 'UuzsG7NMvYFzxwPDdYgLxJbF'
end
what will be the best way to fetch db column and compare it with the link param
You can use the exists? method which pretty much does what it says.
Group.exists?(group_token: params[:token])
You can use include? to check if the param is in the list. For example:
def your_method
list = Model.pluck(:attribute)
list.include?(params[:your_param])
end
in my Rails application I've got a n:m relation between movies and tags. (has_and_belongs_to_many)
So each tag can be assign to several movies.
Now when I add new tags to a movie I want to check If this Tag is already assigned to this movie.
What is the esiest way in rails to check if there is a relation ship between the tag and the movie?
I fetch the tag with:
#tagfound = Tag.where("tagname = ?", data[:tagname])
The List with all Tags from the movie can be fetched with this:
#vid.tags
Thanks for your help
You may not need to check. You can simply do this
movie.tags = [array, of, tags]
movie.save # note, you don't need to save. The line above saves.
or
movie.tag_ids = [1,2,3,4]
movie.save # note, you don't need to save. The line above saves.
and that will take care of it setting new tags and removing the ones that are no longer connected. Good for checkbox UI or a tokenizer.
To answer your question, to find if a movie has a tag, you can do this
tag.in?(movie.tags)
And this is the way to add a single
movie.tags << tag unless tag.in?(movie.tags)
[EDIT]
If you do this
movie.update_attributes(movie_params)
and one of the params is the tag_ids, the movie will only save the new tags if it is valid (no other errors).
I believe there are 2 ways you can do this.
Check if #tagfound is included in #vid.tags
#vid.tags.include? #tagfound
Add the tag & call uniq after.
#vid.tags << #tagfound
#vid.tags.uniq!
I have a hash H which should contain various users as json. "users" list contains all users and every user object contains various user's details like name, age etc. I don't want to iterate over every user in the list and do user.as_json and then merge into the hash.
Is there a single line query which does this?
You can do this, in your action convert it to JSON by using to_json
#users = users.to_json
In the Javascript code, parse it using jQuery's $.parseJSON()
$.parseJSON(#users)
I found a sample code like the title of this post and have a question.
Why is this written like params[:member][:user]?
I got advice from my friend and he explained that [:member] means table name and [:user] means column name included in the table of member.
But I don't get it because table is always plural and it is obvious that he or she are trying to search in table members as showed Member.find.
It is written as params[:member][:user] because params is a nested hash, such as this:
params = {"utf8"=>"✓",
"authenticity_token"=>"uAbvJ/LE1f8eDcANe+TVip5nsWfP/xJxxoGmsQyKFnU=",
"access_token"=>"",
"member"=> {"name"=>"foobar",
"email"=>"foo#bar.com",
"user"=>"jimmy",
"session"=>"2013-01-17 13:15:00 UTC"},
"commit"=>"Submit",
"locale"=>"es"}
This means to get to the value of the user inside of member, you would need to write something like params[:member][:user]. This is typical behavior of forms submitted by Rails, as the model data will be in it's own hash as member is in this example.
I have a piece of controller code where some values are calculated. The result is in the form of an array of hashes. This needs to get into a partial form somehow so that it may be retrieved later during commit (which is through the Submit button).
The questions is how do we pass the array of hashes?
thanks.
Is there a reason it has to be through the form? This is the type of thing I usually use the session for.
I can't really think of a nice way to do what you're asking with forms. I guess you could create hidden fields for each key in your hash in the form with hidden_field_tag as an alternative. Then you run into problems translating it (what if a key's value is an array or another hash?).
You could easily store the hash in the session and then on each page load, check to see if there is a hash where you expect it. On calculating values:
session[:expected_info] = results
And each page load, something like this:
if session.has_key?(:expected_info)
results = session.delete(:expected_info)
# you already calculated the results, just grab them and
# do what you need to do
else
# you don't have the expected info
end
You should be able to pass it as a string to your partial:
[{}].inspect
and eval it when it is submitted back through the form:
eval("[{}]"))
but that would be really dirty…