I got the following deprecation warning on the rails console:
DEPRECATION WARNING: Having additional attributes on the join table of a
has_and_belongs_to_many association is deprecated and will be removed in Rails 3.1.
Please use a has_many :through association instead.
The issue lies with the roles_users table that I created following an online step-by-step tutorial.
How do I implement a has_many :through association for acl9? It's beyond me, especially since the user and role models each only use helper methods and no actual has_and_belongs_to_many.
This is how they look like:
class User < ActiveRecord::Base
acts_as_authentic
acts_as_authorization_subject :association_name => :roles
end
class Role < ActiveRecord::Base
acts_as_authorization_role
end
The answer was later discussed in the comments to this GitHub issue.
User model:
acts_as_authorization_subject :association_name => :roles, :join_table_name => :roles_users
Role model:
acts_as_authorization_role :join_table_name => :roles_users
Also, for the record, Rails decided not to deprecate the :join_table option for habtm after all, so this went away with a subsequent patch release of Rails - ie. you shouldn't need the options mentioned in the issue if you just upgrade your Rails.
Related
I am working on a basic rails app and wondering how do I show /users/:id/posts/:id as /posts/:id. Right now my routes is
resources :users do
resources :post
end
Which i need to have so that every post gets associated by the user. But also at the same time for ease of use, i need to be able to show the post at /posts/:id
Any help of suggestion is appreciated.
Thanks
You can declare a second route to the same controller:
resources :users do
resources :posts
end
resources :posts
You just need to handle not having a user_id parameter in your posts controller gracefully.
you probably looking for shallow option in Rails
resources :users, :shallow => true do
resources :post
end
http://rails-bestpractices.com/posts/11-needless-deep-nesting
In my application, a User has many Posts, and a Post has many Comments. How should I configure the routing? I have configured it like this:
resources :users do
resources :post do
resources :comments
end
end
Some articles say this isn't recommended because it will be confusing.
Yes, It is not recommened. But Allowed in rails
You should use something like this
resources :users do
resources :posts
end
resources : posts do
resources :comments
end
As Deep Nesting Will create problems like, long path names in rails, and long urls in urls.
This is difficult for me to explain, but I need to add the belongs_to attribute to a model of a gem.
I am using the APN_on_rails gem to add push notification functionality to my web app. In this gem it creates a model called Device (APN::Device) which has a few fields needed for it to work.
I now need to link the Device model to my own User model. I have successfully migrated a user_id to the database table.
Now the only problem is adding belongs_to to the model. It is not in die /app/models/ directory.
How can I achieve this?
It's Ruby, so you can just reopen the class and add more to it.
Create a device.rb file in your models directory and just add the appropriate belongs_to as if you created the whole model yourself. As long as you don't redefine any important methods without calling super it will supplement their model with just what you need. You may also need to add an attr_accessible or attr_protected to be able to use it depending on how they defined their model.
I hope that helps.
I created rails3.1 app in spine.js using spine_rails (0.0.9) gem and eco gem. I created two spine:scaffold in my rails app to relate has_many, belongs_to relationship. I included hasMany, belongs_to in spine:model.
I don't know how to get id from one spine:scaffold to another spine:scaffold.
I'm having trouble with a plugin on a rails 3 app called vote_fu_rails_3. This is a rails 3 compatible version of vote_fu by peteonrails. Both plugins are inspired by acts_as_voteable.
I have a site you can look at http://rubeddit.heroku.com - it's a reddit clone for a class i'm doing.
I am trying to get voting working. I have setup everything according to the instructions on the github page. I have a Link model that has_many :votes and my vote model has belongs_to :user and belongs_to :link.
When trying to do the method to vote_for a link by trying <%= link_to 'up', #user.vote_for(#link) %> I get a SQLite constraint error that voteable_id cannot be NULL.
You can see my code here am i doing something incorrectly? I'm open to any suggestions, i'm pretty new with rails.
Edit: The main issue i'm having seems to stem from voteable_id and voteable_type. These are from polymorphic associations. Since I only have the need to vote on Links (a single model) I guess I have the option of removing the polymorphic associations. Leaving them in place however would also make allowing Comments (yet to be added) to be voted on, fairly simple.