A
A
Artyom Lagun2014-01-29 13:54:08
Ruby on Rails
Artyom Lagun, 2014-01-29 13:54:08

Data sampling for the user?

There are tables

user (:id, :username)
follow (:user_id(кто подписался), :following(на кого подписался), :status(статус подписки))
image (:image, :user_id)
comment (:image_id, :user_id, :text)
repost (:user_id, :image_id).

How to make a selection to receive the user's news so that the posts of the users to whom he subscribed are included?
I select user records like this:
@news = User.where('users.id = ?', params[:user][:id]).includes(:images, :reposts).first

And another question: I'm using the rabl gem to output data to json. for the records of the user himself, here is the following code:
object @news
attributes :id, :username

child(:images) do
    attributes :id, :image
    child(:comments)  do
        attributes :id, :text
        child(:user) {attributes :id, :username}
    end
end

child(:reposts) do
    child(:image) do
        attributes :id, :image
        child(:user) {attributes :id, :username}
        child(:comments)  do
                    attributes :id, :text
                    child(:user) {attributes :id, :username}
        end
    end
end

How to display the data of the second object in the same view?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
linch, 2014-01-30
@linch

@news = Repost.where('id IN (?) OR user_id = ?',  Follow.where(user_id: params[:user][:id], params[:user][:id]).preload(:images).limit(20)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question