Forum: Ruby on Rails Creation of object which doesn't have model, while creation need to create several entries in other

236dd7ca555806d7c971f1af1523a3d5?d=identicon&s=25 Srdjan Cengic (Guest)
on 2013-11-09 22:21
(Received via mailing list)
In application user can enter new *post* which contain *title*,
*content*of the post and
*category* of post. So creating new post will be through some simple
form with few fields. Now i don't know where to put logic for creating
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,
             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:
place is more appropriate to put logic for creating new post?

My solution is to craete Storages controller with resource: storages,
=> [:new, :create] then through new and create of this controller to
populate different tables in db? I'm forcing here only because i dont
any point of other CRUD actions here(like showing all or one storage),
because i will not use storages individually but in conjunction with
tables. So from views/storages through new.html.erb and create.html.erb
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
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.

*Question3*I assume that i need tableless-model for post?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.