Ruby on Rails models relationship - ruby-on-rails

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

Related

Rails: set up polymorphic associations for two user types

I'm starting to code a linkedin-like website in Rails for my thesis that would enable members to post job offers (employers) and other members (employees) to respond to them.
I've been trying to wrap my head around the data model and associations - because I don't want to have two channels of authentication for emploYers and Yees - I want to keep email, nickname and password in one table (model) and use that for logging in, and then from there go to Employer and Employee specific data.
I was reading about STI but I figured that Yer has very different data than Yee and there would be a lot of nulls in the Users table, which isn't quite an optimal thing I suppose.
Then I've stumbled across polymorphic associations, but I don't really know how to set them up.
I was thinking of something like this:
User < ApplicationRecord
has_one :employer, polymorphic: true
has_one :employee, polymorphic: true
end
Employee < ApplicationRecord
end
Employer < ApplicationRecord
end
My head boils cause I don't know what's the best way to achieve this, any tips much appreciated,
cheers
You don't need to use the polymorphic association here.
User < ApplicationRecord
has_one :employer
has_one :employee
def employer?
employer.present?
end
def employee?
employee.present?
end
end
Not sure if this would be the best solution in your case though.

Ruby on Rails- Adding Comments to a Post

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.

How can I find a puzzle the user has not yet solved in my Rails 4 app

I'm building a simple puzzle game to learn Rails 4. I have two models in my Rails app, Users and Puzzles. I'm trying to understand how to structure the two models so that I can keep track of the puzzles a User has solved and be able to find an unsolved puzzle for him to play. The puzzles do NOT have to be solved in any order, so my app just needs to be able to find ANY puzzle the user has not yet solved.
One obvious way is to create a many-to-many relationship between Users and Puzzles and create an attribute on the User model that stores the IDs of puzzles he's already solved, then use a simple DB query to find a puzzle ID not in that list, but that feels inefficient...
#Untested code, apologies for typos/bugs
class User < ActiveRecord::Base
has_and_belongs_to_many :solved_puzzles, class_name: "Puzzle"
def unsolved_puzzle
solved_puzzle_ids = self.solved_puzzles.map {|p| p.id}
unsolved_puzzle = self.puzzles.where("id NOT IN ?", solved_puzzle_ids).first
end
end
class Puzzle < ActiveRecord::Base
has_and_belongs_to_many :solved_by, class_name: "User"
end
Are there any problems with this approach? Other thoughts?
Thanks in advance for your wisdom!
If you use has_and_belongs_to_many you can only store the link between user and puzzles. I would prefer has_many :through association where you can save the state of the puzzle for that particular user . The relationship between an user and a puzzle now have a separate class with solved state.
(Untested code)
class User
has_many :user_puzzles
has_many :puzzles, through: user_puzzles
def unsolved_puzzle
user_puzzles.where(solved: false).first
end
end
class UserPuzzle
belongs_to :user
belongs_to :puzzle
#has attribute solved
end
class Puzzle
has_many :user_puzzles
has_many :users, through: user_puzzles
end

Must i use a polymorphic setup?

I have a app setup question: (for a travel site)
My models are
class User < ActiveRecord::Base
end
class House < ActiveRecord::Base
end
class Apartment < ActiveRecord::Base
end
class Boutique < ActiveRecord::Base
end
class City < ActiveRecord::Base
end
class Activity < ActiveRecord::Base
end
I want to add a "i've been there", "i've done that", "i want to go there" ect to the user model. So when a user/visitor is by example on the activity show page there is a function where they can add the status describe above.
Where do i start in relation with my db setup...must i use the polymorphic setup?
Thanks..remco
I believe we need more information as to how the models relate to each other rather than just saying "I want to add I've been there, I've done that". We need relevant information i.e. A user has_many ???? A house belongs_to ???. Please see ActiveRecord Association basics. This will help gain some more clarity on what you are trying to do.

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