Rails scope join with another model scope? [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
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

Related

Coredata chained Relationship [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
What is the best way to update or add data if relationships are chained.
I have my coredata designed like this:
I have 3 Entities
1) User To_Many Category
2) Category To_Many Item
3) Item To_One Category
User is required before adding a Category, and Category is required before adding an Item
Simple. Just set the to-one relationship, the corresponding inverse relationship will be set automatically by Core Data.
newItem.category = category
category.user = user
According to best practice, I am renaming the clumsy attribute names itemCategory, categoryUser, categoryInUser to simply category, user, categories respectively.

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

Should I create two similar tables or one? [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 9 years ago.
Improve this question
In my database I have flights wich are formed by two air tickets in both sides. So, to create flight object I should create two tickets in both sides. The tickets have field in database "flight direction" with two values: 1)"there" 2)"from". I can't figure out how to make form where i can create two tickets with different sides at one time.
You can achieve this by using a callback in your Flight model. This callback will be executed after the Flight is created (= initialized and saved to database).
class Flight
has_many :tickets
...
after_create :create_tickets
def create_tickets
tickets.create(flight_direction: 'from')
tickets.create(flight_direction: 'there')
end
end
This will automatically create two Ticket records in database that are associated with the Flight record.

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)

Simplifying limit and order AR 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 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

Resources