I have done this
def show_selected_students(selected_students, students)
student = students.map{|a| a.name}
selected_students = selected_students.split(",")
#student_selected = selected_students.map {|i| student[i.to_i] }
end
in students I am fetching given data
#<ActiveRecord::Relation [#<Student id: 1, name: "XYZ",>, #<Student id: 2, name: "test1">, #<Student id: 3, name: "cherry">, #<Student id: 4, name: "mary">, #<Student id: 5, name: "hary">, #<Student id: 35, name: "hen">, #<Student id: 44, name: "duck">, #<Student id: 62, name: "try">]>
and in selected_students I am getting 2,3,4 Now I want to fetch those students whose id match with selected_students for this I had written this but it gives me this output ['cherry', 'mary', 'hary'] i.e id 3,4,5 but I want 2,3,4 Please guide me how to solve this. Thanx in advance.
You can try this:
def show_selected_students(selected_students, students)
selected_students = selected_students.split(",")
#student_selected = students.where(:id => selected_students).map{|a|a.name}
end
Related
I have Many-to-many relations with custom properties like here
I try to get a post with all category like
PostRepository.findOne({
relations: ['postToCategories.category'],
where: { id: 1},
})
the results like this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, category: {id: 1, name: "cate1"}}]
}
But i don't want id of intermediate table. I need something like this
{
id: 1,
name: 'post1',
postToCategories: [{category: {id: 1, name: "cate1"}}]
}
or this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, name: "cate1"}]
}
I don't need id of postToCategories. How to do that thank.
I have a large set of #clients that I'd like to sort by the client's last name but I cannot seem to get it working. I need to select :name and :id to speed up the query.
#clients = Client.all.select(:name, :id)
#clients.sort { |a, b| a.name && b.name ? a.name.split(" ").last[0] <=> b.name.split(" ").last[0] : a ? -1 : 1}
The result I get is:
#<ActiveRecord::Relation [#<Client id: 460, name: "Jim Jimmy">, #<Client id: 440, name: nil>, #<Client id: 231, name: "Paigetest Doyle">, #<Client id: 441, name: "Jeremy Lopez">, #<Client id: 462, name: "blah blah">, #<Client id: 348, name: "Jan Aldrich">, #<Client id: 464, name: "fefw fewfew">, #<Client id: 466, name: nil>, #<Client id: 67, name: "Jeremy Lopez">, #<Client id: 449, name: nil>, ...]>
There's a cool feature called split_part in Postgres that allows to split a string and get the word on specified position.
I believe the last_name is on the second position, hence SQL syntax will look something like this.
SELECT *, split_part(name, ' ', 2) AS last_name FROM clients ORDER BY last_name;
ActiveRecord syntax is going to look like:
Client.select("id, name, split_part(name, ' ', 2) as last_name").order("last_name")
I have no access to your env, so here can be some mistakes, but I wanted to make sure you got the idea.
Hope it helps.
When I call:
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
I get this active record realtion:
[#<CodeLesson id: 2, name: "Python", permalink: "python", lesson_content: "", instructions: "Print your name to the console.", hints: "", starting_code: "\"\"\"\r\nThis is a comment\r\n\"\"\"\r\n\r\nprint(\"Hello, World\"...", language_id: "12", order: 1, track_id: 2, user_id: 1, created_at: "2014-02-14 16:01:12", updated_at: "2014-02-15 21:14:43", visible: true>, #<CodeLesson id: 8, name: "Test Lesson", permalink: "test-lesson", lesson_content: nil, instructions: nil, hints: nil, starting_code: nil, language_id: "26", order: nil, track_id: 2, user_id: 1, created_at: "2014-02-20 19:23:15", updated_at: "2014-02-20 19:23:15", visible: false>]
How do I convert this into a usable array of models so I can do something like this:
preivous_lessons.each do |i|
highest = i.order if i.order > highest
end
As OP confirmed from my comment, that my hint solved his problem, I am putting it as an answer to the post :
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
highest = preivous_lessons.maximum(:order)
Documentation of maximum :
Calculates the maximum value on a given column. The value is returned with the same data type of the column, or nil if there's no row.
preivous_lessons = #item.where("track_id = ?", lesson.track_id).all
I'm having problems getting the acts-as-taggable-on gem working properly.
I have a model:
class Resource < ActiveRecord::Base
acts_as_taggable
end
I can add tags to it, and though its tag_list is populated:
$ a = ArchiveResource.new()
$ a.tag_list = ["one", "two", "three"]
$ a.tag_list # ["one", "two", "three"]
However, its tag association is not:
$ a.tags # []
If I check for all the tags, I can see they are being created:
$ ActsAsTaggableOn::Tag.all #<ActsAsTaggableOn::Tag id: 1, name: "one">,
#<ActsAsTaggableOn::Tag id: 2, name: "two">,
#<ActsAsTaggableOn::Tag id: 3, name: "three">
And if I check the model's taggings association I can see they exist there:
$ a.taggings #<ActsAsTaggableOn::Tag id: 1, name: "one">,
#<ActsAsTaggableOn::Tag id: 2, name: "two">,
#<ActsAsTaggableOn::Tag id: 3, name: "three">
Looking more closely at a call to a.tags.all I can see from the query that there is a mismatch:
ActsAsTaggableOn::Tag Load (0.9ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 1 AND "taggings"."taggable_type" = 'ArchiveResource' AND (taggings.context = ('tags'))
However the taggings_context of the model's Taggings are all set to tag singular, so the query always fails:
<ActsAsTaggableOn::Tagging id: 1, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20
<ActsAsTaggableOn::Tagging id: 2, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20
<ActsAsTaggableOn::Tagging id: 3, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20
If I run through all the Taggables and set context to tags, everything works:
ActsAsTaggableOn::Tagging.all.each{|t|t.context = "tags"; t.save!}
So why is this happening. Is it a bug or am I doing something wrong?
Looks like the default implementation is broken. By explicitly declaring the tags as :tags everything works OK:
acts_as_taggable_on :tags
#products = [#<Product id: 15, name: "abcd", revision: 100>,
#<Product id: 19, name: "test", revision: 1080>,
#<Product id: 5, name: "abcd", revision: 109>,
#<Product id: 450, name: "test", revision: 9>,
#<Product id: 2, name: "example", revision: 150>]
Question: I want to output all the products but the only condition is that if the name is duplicated more then once, then I would want to output the product which has the latest revision.
Expected Output:
#products = [#<Product id: 19, name: "test", revision: 1080>,
#<Product id: 5, name: "abcd", revision: 109>,
#<Product id: 2, name: "example", revision: 150>]
As there were two products named "abcd" it should display the one with the latest revision and the same applied to "test", but as there was no duplicates for "example" it is rendered normally.
Any help will be really appreciated, have been stuck on this.
This should do the trick:
temp_array = #products.group_by(&:name)
#filtered_products = temp_array.map do |name, products|
products.sort{ |p1, p2| p2.revision <=> p1.revision }.first
end
Don't hesitate to ask details if you need ;)
ActiveRecord only:
Product.select("name, MAX(revision) AS max_revision, COUNT(*) AS count")
.group("name")
.having("count > 1")