Simplifying limit and order AR query [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there a simpler and shorter way to declare this in Rails?
Message.where(conditions).limit(10).order("created_at desc")

There isn't any way to make the code shorter, but you could add a scope if you want a better interface:
Message < ActiveRecord::Base
scope :my_scope, where(conditions).limit(10).order('created_at DESC')
end
Then you would simply query using Message.my_scope

Related

How to Convert SQL Query to Rails Active Record Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Query statement
select * from timetables where user_id IN
(SELECT user_id FROM timetables GROUP BY user_id HAVING COUNT(*) > 1)
I don't want to use find_by_sql.
because find_by_sql returns array.
I want like to use a mixture of (.where, .group, .having...)
help me...
The problem with this approach is that the result of subquery would be interpreted by ruby, not by MySQL, drastically slowing down the performance, but here you go:
Timetable.where(user_id: Timetable.group(:user_id)
.having('count(*) > 1')
.pluck(:user_id))
#⇒ Timetable::FriendlyIdActiveRecordRelation < ActiveRecord::Relation

Does the include? method work with an array of objects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Say I have some object array:
employees:[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
Can I use Ruby's include? method to do something like this:
employees.include?("'firstName' = 'John'")
Use Enumerable#any? instead:
employees.any?{ |e| e["firstName"] == "John" }
Enumerable is an included module of Array so that should work with arrays as well.
This assumes JSON code has been parsed from raw JSON string to Ruby arrays and hashes.

Rails scope join with another model scope? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a situation i need to join the 4 scopes among the 4 scopes 3 are written in model(A) and one in another model(B) how to join those scopes?
those two models have HABTM relationship
Model A
scope 1
scope 2
scope 3
total_scope= scope1.scope2.scope3.scope4
end
Model B
scope 4
end
Your question isn't very well worded, but I think what you're asking is related to Active Record's merge feature:
class ModelA
scope :total_scope, -> { scope1.scope2.scope3.joins(:modelb).merge(ModelB.scope4)
end

Access all submodels from a ActiveRecord relation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to write something like this
User.groups.members.addresses
What I need is an array of all addresses which User has access to. If User is in two groups, each group has 2 unique members with unique addresses I want an array of 4 addresses
Using Ruby on rails 4
You should be able to add a scope to your address model, you just need to add some joins in there. Haven't tested this but it should be on the right track.
class Address
scope :by_user, -> user { joins(:member).joins(:group).where(user: {id: user.id})}}
end
usage:
Address.by_user(#user)

Should I split this model and table? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I would like to create simple ResumeBank app.
Issue:
As user I would like to add only two Resumes.
Forms for this both Resumes are different with only two fields.
Resumes have 12 the same attributes but 2 are diferent.
Question:
Should I split that Resume model and tables to ex: PolishResume and EnglishResume, polish_remsumes and english_remsumes?
Or maybe should I use STI and create PolishResume < Resume and use one table.
What are disadvantages of splitting option?
Seems like classic inheritance should solve it
class ResumeBase{...}
class ResumeWith12Forms: public: ResumeBase{
//use options to determine which unique 2 forms to show
//options could be an enum or even boolean
ResumeWith12Forms(options){ };
}
class User{ std::vector< std::shared_ptr<ResumeBase> userResume; }

Resources