Ruby on Rails- Adding Comments to a Post - ruby-on-rails

I am making a website using ruby on rails which allow users to submit a recipe. I have called it Recipeazy. I am not sure how I can allow users to add comments (which the user who submitted a comment can edit, and delete the comment).
This is a link to my code: https://ide.c9.io/kingsong/recipeazy
And I'm not sure if this will work for me:https://thinkster.io/tutorials/rails-json-api/adding-comments-to-articles
If I am not clear, please tell me if there is any code I should post.
Thanks.

Neither of the two links you provided work.
What you are trying to accomplish is very easy!
This is a simple commenting setup. This consists of a Recipe model, Comment model, and User model. All these models will be related to each other.
models/recipe.rb
class Recipe < ApplicationRecord
has_many :comments
end
models/user.rb
class User < ApplicationRecord
has_many :comments
end
models/comment.rb
class Comment < ApplicationRecord
belongs_to :recipe
belongs_to :user
end
Like I said, this is a simple commenting setup. This does not address authorization, or nested comments.

Related

Active Record includes with a belongs_to and has_many doesn't return associations

I have the following models:
user.rb
class User < ApplicationRecord
has_many :invitations
end
organization.rb
class Organization < ApplicationRecord
has_many :invitations
end
invitation.rb
class Invitation < ApplicationRecord
belongs_to :user
belongs_to :organization
end
I'm trying to query Active Record in the following way:
user = User.find(params[:id])
user.invitations.includes(:organization)
I want to be able to get all invitations for the user and also have the invitations include attributes of their related organization. However, I am only getting the invitation and none of the organization's attributes.
Even if I try:
Invitation.includes(:organization)
I'm still not getting each invitation's associated organization.
Any and all help is greatly appreciated!
includes method provides eager loading. This solves the N + 1 queries problem. You can access the loaded organization like user.invitations.first.organization. There will be no new queries here.
If you want to combine invitation and organization attributes, you can use joins and select.
user.invitations
.joins(:organization)
.select('invitations.*, organizations.foo, organizations.boo as blabla')
#demir has already answered your question.
Just as an addition - probably, you mixed up includes for models with include option of to_json \ as_json.
If you want to return some JSON result (e.g. in your API response), then you can do user.as_json(include: [invitations: { include: :organization }])

Struggles with has_many and belongs_to association when relationships are 2-3 deep

I'm pretty new to Rails and setting up associations, so I suspect I'm missing something pretty obvious. I'm trying to set up an app where one model has two models that it has_many of. The second model belongs_to the first and has_many of the third. And the third can either belong to the first or the second model.
Specifically, I have a wall model that holds pictures and collages. The wall can hold either pictures or collages or neither. Collages can hold pictures.
class Wall < ActiveRecord::Base
belongs_to :user
has_many :collages
has_many :pictures
end
class Collage < ActiveRecord::Base
belongs_to :user
belongs_to :wall
has_many :pictures
end
class Picture < ActiveRecord::Base
belongs_to :user
belongs_to :wall
belongs_to :collage
end
The error I'm getting is telling me:
undefined method `picture?' for #Wall
Is there something I'm doing wrong with the associations I'm creating?
has_many association on any model gives plural form of that method
Therefore Wall class has method #pictures available by this line:
If you want #picture method to be available you should use association as belongs_to
We can debug more into the exact problem if you tell where actually you are getting this error and what is your feature to implement.
Also name for Picture class should be with capital P
#cvibha's answer should help you with the associations
However, there's another problem you may need to consider. You're calling this method:
undefined method `picture?' for #Wall
Rails associations basically create a record as per how you define the association (has_many :pictures creates #wall.pictures). However, you're calling picture?
--
If you've got a custom method called picture?, this should work (albeit without the association working - as described in the other answer). The problem you have is I don't think you've defined picture?
I would do this:
#app/models/wall.rb
Class Wall < ActiveRecord::Base
...
def picture?
#your code
end
end
Alternatively, if you're looking to validate the existence of a picture, you may wish to use in your view:
#wall.pictures.any?
#wall.pictures.first.present?

Has many polymorphic resources (with ActiveRecord)

Let's say we use ActiveRecord and there's a user (User model) having many comments (Comment model) and many articles (Article model). We can write this:
class User < ActiveRecord::Base
has_many :comments
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
end
class Comment < ActiveRecord::Base
belongs_to :user
end
...so I can do user.comments and user.articles:
user.comments # => [#<Comment:0x12bfcd010>, #<Comment:0x3928c1101>]
user.articles # => [#<Article:0x10aacd333>]
Considering Article and Comment as an item, is that possible to do so (with 1 query)?
user.items # => [#<Comment:0x12bfcd010>, #<Article:0x10aacd333>, #<Comment:0x3928c1101>]
...This way, an item could looks like a polymorphcal attached resource. Even if it's not a normal polymorphic association.
I think this would be possible. Thanks.
This has been asked before, best to google for reverse polymorphic associations. That said, here's the question asking the same thing.
Reverse Polymorphic Associations
And the answer then links to this gist, which illustrates a way to do it pretty well.
https://gist.github.com/1242485

Ruby on Rails models relationship

need some advice.
I'm doing a project on RoR, and do not sure what relationship between the models should I use. I've got three models - Users, Boards and Messages.
The beginning is pretty simple:
User has one Wall, and it belongs to the User, so I guess this should be:
class User < ActiveRecord::Base
has_one :board
end
class Board < ActiveRecord::Base
belongs_to :user
end
The last model is Messages and here comes my problem. Message belongs to User cause he writes it, but it also belongs to a Wall cause he writes it on a wall (and it can be Wall that belongs to other user).
I used the simple solution:
class Theme < ActiveRecord::Base
belongs_to :board
belongs_to :user
end
class User < ActiveRecord::Base
has_one :board
has_many :themes
end
class Board < ActiveRecord::Base
belongs_to :user
has_many :themes
end
But I not satisfy with it, and feel that it isn't perfect. I'm looking for a solution that will let me write thinks like:
user.themes.create(:board => #board)
(now it doesn't fill user_id field)
I hope that isn't a hard task for those who more experienced than me in Ruby on Rails model. I'll appreciate good advices, thanks.
For normal you use some authentification gem like devise. Then you have the current_user variable which includes the object of the user that is currently calling the action.
Then when a user creates the Topic you add one simple line to the controller to set the user:
#theme.user = current_user
You should also use a gem like cancan to manage the authorisation in a cenral file. Youl find a railscast here:
http://railscasts.com/episodes/192-authorization-with-cancan

ActiveRecord association model

I am new to rails and read this guide to get all the info so far.
I have a simple scenario and want to make sure whether or not my associations will work fine.
Scenario:
User logs in -> sets up many groups -> each group has many employees
User model:
class User < ActiveRecord::Base
has_many :groups
end
Group model:
class Group < ActiveRecord::Base
belongs_to :user
has_many :employees
end
Employee model:
class Employee < ActiveRecord::Base
has_many :groups
belongs_to :group
end
Questions
Will this work for the scenario I mentioned?
I am confused about how to get all Employees under a User. What would be the code for that?
If I need typical CRUD for all these models then would would be in my Action? index/create/update/destroy? Can someone point me to a good guide on actions?
I also like the has_many through --
class User < ActiveRecord::Base
has_many :groups
has_many :employees, :through=>:groups
end
Then you can go:
user = User.find(23)
user.employees.do_something
Otherwise, you could loop through your groups and its employees (kinda ugly, but will work)
User.first.groups.each{|group| group.employees.each{|employee| puts employee.name}}
You have it together, for the most part, but I think you need to look at has_and_belongs_to_many (which you will frequently see abbreviated as habtm.) Index, create, update, and destroy would be your CRUD list for Ruby on Rails. As for a good guide, I like Agile Web Development With Rails, by Dave Thomas. (When I'm picking up a new topic, I like books - electronic or otherwise.) It's available online through The Practical Programmers. The question about "what's a good guide" is pretty subjective, so caveat emptor.

Resources