我深深嵌套的資源下面的相關數據:如何從深度嵌套資源
resources :venues, shallow: true do
#Halls
get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
get "hall/:id/visit" => "halls#visit", as: :hall_visit
get "structure", :to => "venues#venue_structure"
resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
resources :halls do
resources :webcasts
resources :booths do
resources :chats
end
end
end
我想用檢索屬於特定用戶的所有events
行activerecord
下面我試過,但這個似乎沒有被產生,我想其結果是事件數據:
def all_events
@events = User.joins(:venues => [:events]).where("venues.user_id" => current_user.id).select("events.*, users.*")
render json: @events
end
上述生成:
{
users: [
{
id: 1,
email: "eeee",
title: "Mr",
first_name: "aaaa",
last_name: "cccc",
position: "Web Developer",
work_phone: "123456",
company: "aaasszz",
sign_in_count: 60,
last_sign_in_ip: "127.0.0.1",
confirmed_at: "2013-10-29T12:26:00.583+11:00",
created_at: "2013-10-29T12:23:25.453+11:00",
roles: [
3
]
},
{
id: 1,
email: "email",
title: "Mr",
first_name: "aaa",
last_name: "bbb",
position: "Web Developer",
work_phone: "123456",
company: "sss",
sign_in_count: 60,
last_sign_in_ip: "127.0.0.1",
confirmed_at: "2013-10-29T12:26:00.583+11:00",
created_at: "2013-10-29T12:23:25.453+11:00",
roles: [
3
]
}
]
}
我想在JSON而非事件數據...
我還使用主動型串行。
這個問題的最佳方法是什麼?
事件模型:
class Event < ActiveRecord::Base
resourcify
belongs_to :venue
validates :name, :start, :finish, presence: true
has_one :event_image, ->{where(:image_type => UploadedFile::IMAGE_TYPE_EVENT_IMAGE)}, as: :imageable, class_name: 'UploadedFile', dependent: :destroy
has_one :logo1, ->{where(:image_type => UploadedFile::IMAGE_TYPE_EVENT_LOGO1)}, as: :imageable, class_name: 'UploadedFile', dependent: :destroy
has_one :logo2, ->{where(:image_type => UploadedFile::IMAGE_TYPE_EVENT_LOGO2)}, as: :imageable, class_name: 'UploadedFile', dependent: :destroy
accepts_nested_attributes_for :event_image, :logo1, :logo2, :reject_if => proc { |attributes| attributes['assets'].blank? }
end
顯示你的事件模型的代碼,與事件表的列名 – beck03076
沿@ beck03076我剛剛更新了事件模型 –
所以事件屬於Venue,Venue belongs_to用戶。用戶has_many場地和場地has_many事件。您的輸入是current_user id。所以,你想,所有的事件belongs_to current_user的場地? – beck03076