I am trying to use the ancestry gem which I found by watching an old Ryan Bates Ancestry video and so I set up my my stuff like so:
Migration:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :comment
t.string :author
t.integer :post_id
t.string :ancestry
t.index :ancestry
t.timestamps
end
end
end
Model:
class Comment < ActiveRecord::Base
belongs_to :post
has_ancestry
validates :comment, presence: true, length: {minimum: 15}
validates :author, presence: true
end
** Controller new Action: **
def new
post = Post.find_by(id: params[:post_id])
post.comments.new(:ancenstry => params[:parent_id])
end
So I think I have set up everything correctly. But when I run the following test:
it "should create a nested comment for a post" do
posted_comment = FactoryGirl.create(:comment, post_id: #post.id)
post :create, :post_id => #post.id, comment: {author: #comment.author, comment: #comment.comment, parent_id: #comment.id}
json = JSON.parse(response.body).to_json
binding.pry
parse_json(json, 'comment').to_json.should have_json_path('id')
end
And inspect the json after the binding pry:
{
"comment":{
"id":9,
"post_id":6,
"author":"Adam",
"comment":"Some Really long Valid Comment length of Something.",
"ancestry":null
}
}
The ancestry section is null. I have even tried tried changing parent_id to ancestry but that doesn't help either. Does any know what I am doing wrong? or have any ideas?
post.comments.new(:ancenstry => params[:parent_id])
The key of your hash is misspelled.
Related
I used this guide as a starting point for creating a messaging system from scratch. I had to modify these to handle messages between User and AdminUser. For some reason, whenever I now try to create a new conversation by clicking in my view the following link:
<li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, recipientable_id: admin.id), method: :post %></li>
I encounter the error:
ActionController::ParameterMissing (param is missing or the value is empty: conversation
Did you mean? controller
authenticity_token
action
recipientable_id):
Params are:
=> #<ActionController::Parameters {"_method"=>"post", "authenticity_token"=>"pHmi9kWBLSc5QSJUPQxfNsqSR1fqWCSCBqEVgRMljhgrxB9g4M0ClsdEi2hBLCTjrLLl774T-mnyK8m40LFhNA", "recipientable_id"=>"1", "sendable_id"=>"2", "controller"=>"conversations", "action"=>"create"} permitted: false>
I am directed to the params.permit line in my controller:
class ConversationsController < BaseController
def index
#users = User.all
#admins = AdminUser.all
#conversations = Conversation.all
end
def create
#conversation = if Conversation.between(params[:sendable_id], params[:recipientable_id]).present?
Conversation.between(params[:sendable_id], params[:recipientable_id]).first
else
Conversation.create!(conversation_params)
end
redirect_to conversation_messages_path(#conversation)
end
private
def conversation_params
params.require(:conversation).permit(:sendable_id, :recipientable_id)
end
end
If I remove require(:conversation) I'll get an error:
Validation failed: Sendable must exist, Recipientable must exist
Models:
class User < ApplicationRecord
has_many :conversations, as: :sendable
has_many :conversations, as: :recipientable
end
class AdminUser < ApplicationRecord
has_many :conversations, as: :sendable
has_many :conversations, as: :recipientable
end
class Conversation < ApplicationRecord
belongs_to :sendable, polymorphic: true
belongs_to :recipientable, polymorphic: true
has_many :messages, dependent: :destroy
validates :sendable_id, uniqueness: { scope: :recipientable_id }
end
class Message < ApplicationRecord
belongs_to :conversation
belongs_to :messageable, polymorphic: true
validates_presence_of :body
end
schema:
create_table "conversations", force: :cascade do |t|
t.string "sendable_type"
t.bigint "sendable_id"
t.string "recipientable_type"
t.bigint "recipientable_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["recipientable_type", "recipientable_id"], name: "index_conversations_on_recipientable"
t.index ["sendable_type", "sendable_id"], name: "index_conversations_on_sendable"
end
You need to help Rails understand which polymorphic models you are referencing; if you only provide ids it fails because Rails also needs the polymorphic type (remember: the type is mandatory so Rails can make the link to the actual table. In your case there are two possible types User and AdminUser)
Simply provide the polymorphic types and also add them to the conversation_params method. From looking at your code I'm guessing this is what you're after:
<li><%= link_to admin.email, conversations_path(sendable_id: current_user.id, sendable_type: 'User', recipientable_id: admin.id, recipientable_type: 'AdminUser'), method: :post %></li>
I'm a newbie in RoR and I'm trying to make a simple experiment. I want to have a User model and a Role model. Each user a single Role, but a single Role may refer to multiple Users.
So, here are my models:
class Role < ActiveRecord::Base
has_many :user
end
class User < ActiveRecord::Base
has_one :role
end
And here are migrations:
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name, null: false, index: true, unique: true, limit: 16
t.integer :permissions, null: false
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email, null: false, index: true, unique: true, limit: 128
t.string :password_digest, null: false, limit: 40
t.string :password_salt, null: false, limit: 40
t.string :screen_name, default: '', limit: 32
t.belongs_to :role, index: true, foreign_key: true
t.timestamps null: false
end
end
end
What I want is to make it raise an exception when I try to connect user with a role that does not exist:
user = User.create(email: 'user#example.com', password_digest: 'pwd_dig',
password_salt: 'salt', role_id: 10)
Unfortunately this works and the new user is created, no matter that the role with id 10 does not exist.
So, how can I force foreign_key check here?
And another question about that. If I try to do like this:
user = User.create(email: 'user#example.com', password_digest: 'pwd_dig',
password_salt: 'salt', role: role)
it raises an exception because role does not have attribute user_id. There is no way to do like this, does it?
I'm a newbie in RoR
Welcome!!
#app/models/role.rb
class Role < ActiveRecord::Base
has_many :users
end
#app/models/user.rb
class User < ActiveRecord::Base
belongs_to :role
validates :role_id, presence: true, on: :create
validate :check_role, on: :create
private
def check_role
errors.add(:role_id, "Role doesn't exist") unless Role.exists? role_id
end
end
This will allow you the following:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
#roles = Role.all
end
def create
#user = User.new user_params
#user.save
end
private
def user_params
params.require(:user).permit(:email, :etc, :role)
end
end
#app/views/users/new.html.erb
<%= form_for #user do |f| %>
<%= f.email_field :email %>
<%= f.collection_select :role_id, #roles, :id, :name %>
<%= f.submit %>
<% end %>
Because you're new, I'll give you some info:
1. Association
Your association is slightly incorrect - you'll be best using a belongs_to/has_many relationship:
The belongs_to will assign the foreign_key in your User model, allowing you to both reference and validate against it.
-
2. Custom validator
Secondly, you'll be able to use a validation to check whether the role has been set correctly.
Each time a Rails model is saved, it calls a series of validators you've set -- allowing you to call such functionality as checking whether a role exists.
What you can do is add a custom validation:
class User < ActiveRecord::Base
belongs_to :role
validate :existance_of_role
private
def existance_of_role
if role_id && !Role.exists?(role_id)
errors.add(:role, "must already exist.")
end
end
end
Also you need to use belongs_to :role. has_one would place the foreign_key in the relationship on the Role model instead of on the user.
http://requiremind.com/differences-between-has-one-and-belongs-to-in-ruby-on-rails/
If the user must have a role.
Also if you want to enforce on the database level that the user has a role you would add a NOT NULL constraint to users.role_id.
Run: rails g migration AddNotNullConstraintToUsers
And then edit the migration file created:
class AddNotNullConstraintToUsers < ActiveRecord::Migration
def change
change_column_null(:users, :role_id, false)
end
end
After running the migration you can change the validation so that it will add an error also when the role_id is nil.
private
def existance_of_role
errors.add(:role, "must already exist.") unless Role.exists?(role_id)
end
You could do the same thing with validates_presence_of :role but that will not enforce that the Role is pre-existing.
You can add this validator at your User model
class User < ActiveRecord::Base
has_one :role
validate :role_id_exists
def role_id_exists
return false if Role.find_by_id(self.role_id).nil?
end
end
I am trying to make a forum application with Rails 4. I want users to have many forums and so I know I need a many-to-many relationship. I have a form to save the title and the description of the new forum. I Have 3 tables so far, users, forums, and forums_users. Everything works great when I create a new form and it gets added to the forums database. My question is how do I get the information to go to the forums_users table? Because right now when I submit my form, it does not add the information to the association table.
Here is my migration file for forums.
def up
create_table :forums do |t|
t.string :title
t.text :description
t.string :logo
t.boolean :is_active, default: true
t.timestamps
end
add_index :forums, :title
create_table :forums_users, id: false do |t|
t.belongs_to :forum, index: true
t.belongs_to :user, index: true
end
end
def down
drop_table :forums
drop_table :forums_users
end
These are my models.
class Forum < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :forums
end
Here is my create method in the Forum Controller
def create
#forum = Forum.new(forum_params)
#forum.save
respond_to do |format|
format.html{redirect_to admin_path, notice: 'New forum was successfully created.'}
end
end
private
def forum_params
params.require(:forum).permit(:title, :description, :logo, :is_active)
end
And here is the form you submit.
= simple_form_for(:forum, url: {action: :create, controller: :forums}) do |f|
= f.error_notification
.form-inputs
= f.input :title, autofocus: true
= f.input :description, as: :text
.form-actions
= f.button :submit
Thank you in advance.
If you want to get the data from your join table forum_users then use has_many :through
class Forum < ActiveRecord::Base
has_many :users, through: :forum_users
end
class User < ActiveRecord::Base
has_many :forums, through: :forum_user
end
class ForumUser < ActiveRecord::Base
belongs_to :user
belongs_to :forum
end
Now you can access/fetch the forum_users table data using UserForum Model
Create the forum using a reference to the current user, for example:
#forum = current_user.forums.create(forum_params)
I have a form for a model called isp, which 'has_many' isp accounts. the isp account belongs to to 'isp'.
There is a validation on the isp_account that means it cant be added if there isnt an isp_id, so my reasoning is to created a nested form. I created the nested form like so
= simple_form_for #isp, :html => { :multipart => true } do |f|
= f.input :title
= f.simple_fields_for :isp_accounts do |tag|
= tag.input :title, label: "Tag Name"
however the nested attribute isnt being displayed. There are no errors etc. Why is this? Am I approaching this in the best way? is this the only way?
here's the code
ISP MODEL
class Isp < ActiveRecord::Base
has_many :isp_accounts, dependent: :destroy
has_many :deployments, through: :servers
has_many :servers, through: :isp_accounts
validates :title, presence: true
accepts_nested_attributes_for :isp_accounts
end
ISP ACCOUNTS MODEL
class IspAccount < ActiveRecord::Base
belongs_to :isp
has_many :deployments, through: :servers
has_many :servers, dependent: :destroy
validates :title, presence: true
validate :check_associates
private
def check_associates
associated_object_exists Isp, :isp_id
end
end
ISP ACCOUNT CONTROLLER
....
def new
#isp_account = IspAccount.new
end
def update
#isp_account.update_attributes(isp_accounts_path)
if #isp_account.save
record_saved
return redirect_to(isp_accounts_path)
else
check_for_errors
return render('/isp_accounts/edit')
end
end
private
def get_isp_accounts
#isp_account = IspAccount.all
end
def get_isp_account
#isp_account = IspAccount.find(params_isp_accounts)
end
def params_isp_accounts
params.require(:isp_account).permit!
end
end
....
def new
#isp = Isp.new
end
def update
#isp.update_attributes(params_isp)
if #isp.save
record_saved
return redirect_to(isps_path)
else
check_for_errors
return render('new')
end
end
private
def params_isp
params.require(:isp).permit(:title, isp_accounts_attributes: [:id, :title])
end
def get_isp
#isp = Isp.where(id: params[:id]).first
unless #isp
record_not_found
return redirect_to(isps_path)
end
end
def get_isps
#isp = Isp.all.order(:title)
end
end
SCHEMA
create_table "isp_accounts", force: true do |t|
t.string "title"
t.integer "isp_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "isps", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end
ok i got it. I was missing the new bit for that attribute in my controller. pretty basic really.
def new
#isp = Isp.new
#isp.isp_accounts.new
end
Lets say I have two tables.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :type, :default => 'User'
t.string :user_name, :null => false
t.boolean :is_registered, :default => true
# ... many more fields
end
end
end
class CreateContactInfo < ActiveRecord::Migration
def self.up
create_table :contact_info do |t|
t.integer :resource_id
t.string :resource_type
t.string :first_name
t.string :last_name
t.string :middle_initial
t.string :title
end
end
end
class ContactInfo < ActiveRecord::Base
belongs_to :contactable, :polymorphic => true
end
class User < ActiveRecord::Base
has_one :contact_info, :as => :contactable
# composed_of :contact_info # ... It would be nice if magics happened here
end
I would like to have the User's contact_info automatically merged into my User object as attributes of the user object without having to say #user.contact_info.first_name; instead, I would prefer to be able to write #user.first_name.
The reason I am breaking out attributes to the contact_info table is that these are common attributes to multiple models. That is why I am making setting up the contact_info as a polymorphic association.
Does anyone know of a good way to aggregate/merge the attributes of contact_info directly into my user model?
Use delegate:
class User < ActiveRecord::Base
has_one :contact_info, :as => :contactable
delegate :name, :name=, :email, :email=, :to => :contact_info
end
Not necessarily a good way to do it, but I did something similar by overriding the method_missing method and then calling my aggregated object. So, it would look something like:
class User
def method_missing(method_id)
self.contact_info.send(method_id)
end
end
Edit 1: Better implementation (I think this will work):
class User
alias_method :orig_method_missing, :method_missing
def method_missing(method_id)
if (self.contact_info.respond_to?(method_id))
self.contact_info.send(method_id)
else
orig_method_missing(method_id)
end
end
end
The above has the advantage that all other unknown method calls will get passed correctly.
I finally got it! Thank you both amikazmi and Topher Fangio. I had to implement both the delegate and method_missing techniques to get this to work.
Here is the total madness that finally ended up working for me! If anybody has suggestions on how to further improve this, I'd love to hear your suggestions.
class User < ActiveRecord::Base
attr_accessible *([:user_name, :udid, :password, :password_confirmation, :contact_info] + ContactInfo.accessible_attributes.to_a.map {|a| a.to_sym})
has_one :contact_info, :as => :contactable
def method_missing(method_id, *args)
if (!self.respond_to?(method_id) && self.contact_info.respond_to?(method_id))
self.contact_info.send(method_id, *args)
elsif (!self.class.respond_to?(method_id) && ContactInfo.respond_to?(method_id))
ContactInfo.send(method_id, *args)
else
super(method_id, *args)
end
end
# delegating attributes seems redundant with the method_missing above, but this secret sauce works.
ContactInfo.accessible_attributes.to_a.each do |a|
delegate a.to_sym, "#{a}=".to_sym, :to => :contact_info
end
def initialize(*args)
options = args.extract_options!
contact_attrs = ContactInfo.accessible_attributes.to_a.map{|a| a.to_sym}
#ci = ContactInfo.new(options.reject {|k,v| !contact_attrs.include?(k) })
super(*(args << options.reject { |k,v| contact_attrs.include?(k) }.merge(:contact_info => #ci) ) )
self.contact_info = #ci
end
validates_presence_of :user_name
validates_uniqueness_of :user_name
validates_associated :contact_info
def after_save
# automatically save the contact info record for the user after the user has been saved.
self.contact_info.save!
end
end
class ContactInfo < ActiveRecord::Base
set_table_name "contact_info"
belongs_to :contactable, :polymorphic => true
validates_presence_of :email
validates_uniqueness_of :email
attr_accessible :first_name,
:last_name,
:middle_initial,
:title,
:organization_name,
:email,
:email_2,
:twitter_name,
:website_url,
:address_1,
:address_2,
:city,
:state,
:zip,
:phone_work,
:phone_mobile,
:phone_other,
:phone_other_type
def full_name
[self.first_name, self.last_name].compact.join(' ')
end
end