Manage has_and_belongs_to_many with JsonApiResource - ruby-on-rails

I have a relation has_and_belongs_to_many between class Consultant and class Company with join_table OtherCompaniesOtherConsultants.
I would like to give the possibility to modify the OtherCompanies of a Consultant from the RecruiterResource (I have a model_name in my RecruiterResource to Consultant and another in my EntitiesResource to Company).
I have this lines :
In RecruiterResource : relationship :other_companies, to: :many, relation_name: "consultants_companies"
In EntityResource : relationship :other_consultants, to: :many, relation_name: "consultants_companies"
In routes.rb : jsonapi_resources :entities and jsonapi_resources :recruiters do jsonapi_relationships end
I have this error :
NameError: JSONAPI: Could not find resource 'api/v3/company'. (Class API::V3::CompanyResource not found)

Related

Rails Admin throws No route matches error

I have this configuration:
question.rb
class Question
belongs_to :asker
belongs_to :expert
end
user.rb
class User
has_one :asker
has_one :expert
end
I've setup rails_admin and I'm not able to edit any question because of the error.
ActionController::UrlGenerationError at /question/764/edit
No route matches {:action=>"show", :controller=>"rails_admin/main", :id=>nil, :model_name=>"asker"}
I've tried adding a show method in the askers_controller.rb, but it still doesn't make any difference.
Thanks.
Do you have edit method in askers_controller ?
When you try to edit something it should have path like this edit_admin_news_letter GET /admin/news_letters/:id/edit

relation "admin_keywords" does not exist`

I want to have scaffold actions for Keywords table as admin. This code begins to work after I restart the server and Remove Admin:: from /app/models/admin/keyword.rb, then refresh website, get error and adding Admin:: to model again. From that moment everything works fine. But after server Starts, I got this: (Rails 4)
PG::UndefinedTable: ERROR: relation "admin_keywords" does not exist
/app/controllers/admin/keywords_controller.rb source:
class Admin::KeywordsController < ApplicationController
def index
#keywords = Admin::Keyword.all
end
end
/app/models/admin/keyword.rb source:
class Admin::Keyword < ActiveRecord::Base
end
going to url:
http://localhost:3000/admin/keywords
routes.rb:
namespace :admin do
resources :keywords
end
How to fix this error?
If you add namespace to your models, database table should contain this namespace too. For example model Admin::Keyword is related with admin_keywords table.
You can override model's table defining self.table_name='your_table_name' method in model.
class Admin::Keyword < ActiveRecord::Base
self.table_name = 'your_table_name'
end

Rails routes.rb syntax

I have searched and searched and I cannot find a page which spells out the syntax of routes.rb in Rails 3. There are guidelines, overviews, even advanced examples but why isn't there a page that spells out the exact syntax of each keyword?? This page
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
contains a lot of advanced examples but doesn't take the time to discuss the behavior of all the examples given. I would appreciate it if someone could point me to a page that breaks down the syntax of routes.rb.
Here is the problem I am trying to solve. I have two models modelA and modelB. The relationship is modelA has_many modelB and modelB belongs_to modelA. I created the controller for modelB under namespace of modelA. So in my rails app folder, I have
app/controllers/modelA_controller.rb
app/controllers/modelA/modelB_controller.rb
I want my routes to be as such:
http://localhost:3000/modelA/:modelA_id/modelB/ [index]
http://localhost:3000/modelA/:modelA_id/modelB/:modelB_id [show]
etc.
I tried the following in routes.rb and none of it works:
resources :modelA do
resources :modelB
end
--
resources :modelA do
member do
resources :modelB
end
end
--
namespace :modelA do
resources :modelB
end
--
match '/modelA/:modelA_id/modelB/action', :to => '/modelA/modelB#action'
I know some of the things I tried are obviously wrong but when you have spent 2 days on a single problem, anything goes!
The reason no one has a "definitive" guide on routing syntax is that it is pretty flexible so you could probably write a few chapters just on that one subject. However, I would recommend: http://guides.rubyonrails.org/routing.html
From your question, it sounds like you're namespacing modelB under modelA but you also want the id for modelA to be within the route itself.
So if your ModelBController looks something like:
class ModelA::ModelBController < ApplicationController
# controller code here
end
then you can just do:
resources :modelA do
resources :modelB, :module => :modelA
end
However, are you sure you want to namespace the controller like that? If you just want nested resources like a typical has_many relationship, you don't need to be namespacing modelB under modelA.
Instead, you'd have:
/app
/controllers
/modelA
# some files
/modelB
# some files
And your modelB controller would be:
class ModelBController < ApplicationController
# controller code here
end
Then you could do
resources :modelA do
resources :modelB
end

Wrong sql request done with a "has_many" for a subclass

I'm trying to implement a "facebook like" like relationship.
I wanted to use inheritance for that, so here's my structure :
Likes with a user_id:integer attribute
|> PostsLike that inherits Like with a post_id:integer attribute
So here's my Like.rb model :
class Like < ActiveRecord::Base
belongs_to :user
end
And then my PostLike.rb model :
class Postlike < Like
belongs_to :post
end
And finally I have a post model that will have multiple postlikes objects :
class Post < ActiveRecord::Base
has_many :postlikes
end
But here's my problem, when I go into irc and that :
I get a Post object :
ruby-1.9.2-p290 :001 > #p = Post.all.first
I try to get postlikes of this object, here's the sql statement :
SELECT likes.* FROM likes WHERE likes.type IN ('Postlike') AND likes.post_id = 310
So basically instead of doing
postlikes.post_id
Rails' doing
likes.post_id
Any idea to fix that ?
ActiveRecord stores Like and Postlike object in the same table named "likes", and uses a column named "type" to save the class of the object. That explains the statement:
WHERE likes.type IN ('Postlike') AND likes.post_id = 310
There is no table postlikes
See "Single table inheritance" chapter at http://api.rubyonrails.org/classes/ActiveRecord/Base.html

How to order by in Rails?

I'm working on a small blog engine.
There are the following tables: Blog and Message.
Blog has a foreign key: last_message_id, so I access the last message in the blog by calling blog.last_message
I have the following code to make it work:
class Blog < ActiveRecord::Base
belongs_to :last_message, :class_name => "Message"
end
I need to order the blogs by the last messages. But when I call
blogs.order("last_message.created_at DESC")
It doesn't work. I get the following error:
PGError: ERROR: missing FROM-clause entry for table "last_message"
ORDER BY last_messa...
How can I make it work?
UPDATE
Here's the solution:
blogs.joins(:last_message).order("messages.created_at DESC").
I think your model is wrong. See rails automaticly add 2 attributes to a model : created_at and update_at. So having a relationship like you describe is redondant. To me, it should look like this :
#model/blog.rb
class Blog < ActiveRecord::Base
has_many :messages
end
#model/message.rb
class Message < ActiveRecord::Base
belongs_to :blog
end
Then, to get the blogs ordered by the last message, you could do this :
Blog.joins(:messages).order("messages.created_at_desc")
That as you may have noticed will give you double entries for your blog model. If that is not a problem, go ahead. If it is, you have two options : doing a each and test if you already saw the blog - if not, you display it. Or, you could write your own sql.
You have to make sure the last-messages are also selected to make that order-command work.\
So something like:
blogs.includes(:last_message).order("last_message.created_at desc")

Resources