In application user can enter new post which contain title,
contentof the post and
category of post. So creating new post will be through some simple
html
form with few fields. Now i don’t know where to put logic for creating
new
post for following reasons:
Post(or posts collection) is object which is constructed from different
tables, for example.
@posts = User.joins(entries: [{storage: :vote}, :category])
.where(“votes.count > ?”, 0)
.select(“users.username AS username,
storages.id AS storage_id,
storages.title AS title,
storages.content AS content,
votes.count AS votes,
categories.category_name AS category_name”)
.order(“votes.count DESC”)
So when user create new post application must create new entries in
different tables:
1.Create new entry in entries table. (id, user_id, category_id)
2. Create new entry in storages table.(id, title, content, entry_id)
3. Create new entry in vote table.(id, count, storage_id)
In situation where post is model i can use something like resources:
poststhen in posts controller through
new and create i can create new post, but what in situation like this
where i don’t need posts controller nor post model? So, question is:
which
place is more appropriate to put logic for creating new post?
Question1
My solution is to craete Storages controller with resource: storages,
:only
=> [:new, :create] then through new and create of this controller to
populate different tables in db? I’m forcing here only because i dont
see
any point of other CRUD actions here(like showing all or one storage),
because i will not use storages individually but in conjunction with
other
tables. So from views/storages through new.html.erb and create.html.erb
i
can construct new post? Question2
Another solution is to create Post controller which doesn’t have
“corresponding” post model as i stated before. Here i’m guessing i can’t
use restful routes(CRUD) because there is not :id of post? I only can
make
manually non-restful routes like:
post ‘posts/create/:title/:content/:category’ => ‘posts#create’, :as =>
‘create_post’ then from params[:title], params[:content] and
params[:category] to populate other tables.
Question3I assume that i need tableless-model for post?