rails_admin can't delete HABTM object - ruby-on-rails-4

My two models are
class Team < ActiveRecord::Base
has_and_belongs_to_many :releases
end
class Release < ActiveRecord::Base
has_and_belongs_to_many :teams
end
class ReleasesTeam < ActiveRecord::Base
end
When I try to delete a team or release on rails_admin, I get an error:
undefined method `releases_teams' for #<Release:0x007fe57b0a9f20>
raised on this line
def method_missing(method, *args, &block)
if respond_to_without_attributes?(method, true)
super
else
match = match_attribute_method?(method.to_s)
match ? attribute_missing(match, *args, &block) : super #ERROR RAISED HERE
end
end
What am I missing here?
EDIT:
After Pavan's comment, I removed ReleasesTeam model. DB schema looks like this now:
create_table "releases", force: true do |t|
t.text "notes"
end
create_table "teams", force: true do |t|
t.string "name"
end
create_table "releases_teams", id: false, force: true do |t|
t.integer "team_id"
t.integer "release_id"
end
I reset the DB and still getting the same error

Update Rails to 4.1.4.
Issue
https://github.com/sferik/rails_admin/issues/1984
thebenedict commented
This is a rails issue, not rails_admin. HABTM implementation changed in rails 4 in a way that breaks reflection on association names. There's a fix in 4-1-stable, and upgrading rails to 4.1.4 fixes this for me. Also see rails/rails#14682

Related

Rails 4: polymorphism, single-table inheritance, scoping...?

Suppose the following:
class Customer < ActiveRecord::Base
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
end
Customers can be red customers and/or blue customers, but they are never just a customer. Red customers own dogs, blue customers wear hats (a customer must be hat-wearing or dog-owning). For a given order, red_customer is never equal to blue_customer. Since I never use customer_id, I want to eliminate it from my schema and replace it with red_customer_id and blue_customer_id. Each order has a red_customer_id and blue_customer_id.
Examples...
#some_order.customer # no, bad, do not want
#some_order.red_customer # returns a user object
#some_order.blue_customer # returns a user object
Schema (currently ugly)
create_table "orders"
t.string "name"
t.integer "user_id" # I want to get rid of this
t.integer "red_customer_id"
t.integer "blue_customer_id"
end
But if I remove user_id, then my associations break. How can I clean this up?
Assuming an order can only belong to a single user at a time I do not recommended to have 2 foreign_keys in your table. You can achieve the same result using Single Table Inheritance:
class Customer < ActiveRecord::Base
has_many :orders
end
class RedCustomer < Customer
end
class BlueCustomer < Customer
end
class Order < ActiveRecord::Base
belongs_to :red_customer, foreign_key: 'customer_id'
belongs_to :blue_customer, foreign_key: 'customer_id'
end
This way you keep a single table for customers and a single foreign_key on orders.
For this to work you need a type string column in the customer table, also as the Order migration, add polymorphic: true to the customer foreign_key declaration, this way the column customer_type will also be created.
class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.string :type
t.timestamps null: false
end
end
end
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.references :customer, polymorphic: true, index: true
t.timestamps null: false
end
end
end
It works as expected:
order = Order.create
order.red_customer = BlueCustomer.create #=> raises <ActiveRecord::AssociationTypeMismatch>
order.red_customer = RedCustomer.create #=> Ok
order.blue_customer #=> nil
order.red_customer #=> #<RedCustomer id: ...>
order.blue_customer = BlueCustomer.create #=> Ok
order.red_customer #=> nil
Hope this helps you.

How to upload multiple files without nested form using Rails 4?

Can anybody tell me how can I implement multiple file upload using carrierwave without taking separate model for files?
I have my model as expense_details.rb and I need to upload multiple receipts for those expenses.
You need:
1) a polymorphic attachments resource
2) rails upload gem(https://github.com/carrierwaveuploader/carrierwave)
3) a js multiple uploader(https://blueimp.github.io/jQuery-File-Upload/)
4) easy to use 2 & 3(github.com/tors/jquery-fileupload-rails)
db:
class CreateAttachments < ActiveRecord::Migration
def change
create_table :attachments do |t|
t.string :name
t.string :link
t.integer :attachmentable_id
t.string :attachmentable_type
t.integer :user_id
t.timestamps
end
add_index :attachments, :user_id
add_index :attachments, [:attachmentable_id, :attachmentable_type]
end
end
controller:
class AttachmentsController < ApplicationController
...
def create
#attachment = Attachment.new(params[:attachment])
#attachment.name = params[:attachment][:link].original_filename
if #product_attachment.save
# do something
else
# do something
end
end

dependent: :delete_all association not deleting associated records

I can't understand why my dependent: option isn't deleting my associations...
Rails api docs say this:
"Delete or destroy?
has_many and has_and_belongs_to_many associations have the methods destroy, delete, destroy_all and delete_all.
...
For has_many, destroy and destroy_all will always call the destroy method of the record(s) being removed so that callbacks are run. However delete and delete_all will either do the deletion according to the strategy specified by the :dependent option, or if no :dependent option is given, then it will follow the default strategy. The default strategy is to do nothing (leave the foreign keys with the parent ids set), except for has_many :through, where the default strategy is delete_all (delete the join records, without running their callbacks)."
Okay, so why isn't my ...delete...do[ing] the deletion according to the strategy specified by the :dependent option?
I have a UserProfile model that has a has_many relationship with my CustomSkill model; my CustomSkill model belongs_to a UserProfile. I set up dependent: :delete_all on the association that my UserProfile model has with CustomSkill, but when I delete a UserProfile, the associated CustomSkills fail to get deleted.
P.S. If I use destroy when deleting my UserProfile, it seems to work, but I am trying to avoid using callbacks, which delete/delete_all is supposed to do (if I'm understanding 'callbacks' correctly, running rails model callbacks when a delete takes place, because I don't have them coded anyhow).
What am I missing here?
# Controller#destroy
def destroy
UserProfile.find_by_id(destroy_params).delete
redirect_to profiles_url, notice: 'Profile deleted'
end
# UserProfile model
class UserProfile < ActiveRecord::Base
...
has_many :custom_skills, dependent: :delete_all
end
# CustomSkill model
class CustomSkill < ActiveRecord::Base
...
belongs_to :user_profile
end
# Schema.rb
create_table "user_profiles", force: :cascade do |t|
t.string "type"
t.string "profile_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "custom_skills", force: :cascade do |t|
t.string "name"
t.integer "user_profile_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
delete does not trigger callbacks (including dependent: :delete_all). So when you say
UserProfile.find_by_id(destroy_params).delete
the callbacks for UserProfile are not run (including the callback to delete all associated CustomSkills).
To run callbacks (including dependent: :delete_all), use destroy.
UserProfile.find_by_id(destroy_params).destroy

How do I hide from a user the ability to perform an action on a resource after a period of time in Rails 4?

For my projects table I have a column, state, which take string values: "open", "in production", "pending approval" and "completed". The actions that trigger changes between these states are messages that go to and from users about the projects or actions associated to messages. When the object, project, goes from "open" to "in production", in the users inbox a user has a button appearing on the conversation associated with the project that reads, "Drop Project." Moreover, I can wire up the functionality as well. Clicking this button will disassociate you with the project.
My question: how can I have this button hide itself or disappear after 5 days of the project's state, having gone from "open" to "in production"? In other words, I want the user to have the opportunity to disassociates his self from the project within a certain time limit. After that, he is stuck with the project and is encouraged to finish it.
Also, how would I test this with rspec? Is this strictly with an integration test, or can I test it with a unit test too?
I am aware of this SO post (Rails 3 Check if attribute changed), but it has not helped me figure out the solution to my problem. However, I feel like it could serve as support.
Here is my db schema and models:
ActiveRecord::Schema.define(version: 20140514191454) do
create_table "conversations", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "organizations", force: true do |t|
t.string "name"
t.datetime "ruling_year"
t.text "mission_statement"
t.string "guidestar_membership"
t.string "ein"
t.string "street1"
t.string "street2"
t.string "city"
t.integer "state_id"
t.string "zip"
t.integer "ntee_major_category_id"
t.string "funding_method"
t.integer "user_id"
t.string "cause"
end
create_table "private_messages", force: true do |t|
t.integer "sender_id"
t.integer "recipient_id"
t.string "subject"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "project_id"
t.integer "conversation_id"
end
create_table "project_users", force: true do |t|
t.integer "user_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects", force: true do |t|
t.string "title"
t.text "description"
t.string "skills"
t.string "causes"
t.datetime "deadline"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.integer "organization_id"
t.integer "estimated_hours"
t.string "state"
end
create_table "user_conversations", force: true do |t|
t.integer "user_id"
t.integer "conversation_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.integer "organization_id"
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "interests"
t.string "skills"
t.string "street1"
t.string "street2"
t.string "city"
t.integer "state_id"
t.integer "phone_number"
t.string "zip"
t.boolean "organization_administrator"
t.boolean "organization_staff"
t.boolean "volunteer"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "position"
t.integer "project_id"
t.string "time_zone"
end
end
Conversation
class Conversation < ActiveRecord::Base
has_many :private_messages, -> {order('created_at ASC')}
def sender_user_name_of_recent_message
message = self.private_messages.last
user = message.sender_id
name = User.find_by(id: user)
"#{name.first_name} #{name.last_name}"
end
def the_id_of_sender
message = self.private_messages.last
user = message.sender_id
name = User.find_by(id: user)
name.id
end
def private_message_subject
message = self.private_messages.last
message_subject = message.subject
end
def private_message_body
message = self.private_messages.last
message_body = message.body
end
def join_request
message = self.private_messages.first
project = Project.find_by(id: message.project_id)
if project
project.state == "open"
end
end
def project_complete_request
message = self.private_messages.first
project = Project.find_by(id: message.project_id)
if project
project.state == "pending approval"
end
end
def opportunity_drop_project
message = self.private_messages.first
project = Project.find_by(id: message.project_id)
if project
project.state == "in production"
end
end
end
User
class User < ActiveRecord::Base
has_secure_password validations: false
belongs_to :organization
belongs_to :project
has_many :project_users
has_many :projects, through: :project_users
has_many :sent_messages, class_name: 'PrivateMessage', foreign_key: 'sender_id'
has_many :received_messages, -> {order('created_at DESC')}, class_name: 'PrivateMessage', foreign_key: 'recipient_id'
has_many :conversations
def private_messages
messages = self.sent_messages + self.received_messages
messages.sort!
end
def user_conversations
collection = self.received_messages.select(:conversation_id).distinct
all_conversations = collection.map do |member|
convo_id = member.conversation_id
Conversation.find_by(id: convo_id)
end
all_conversations.sort
end
def organization_name
organization.name
end
end
Organization
class Organization < ActiveRecord::Base
belongs_to :organization_administrator, foreign_key: 'user_id', class_name: 'User'
has_many :projects
has_many :users
end
PrivateMessage
class PrivateMessage < ActiveRecord::Base
belongs_to :recipient, foreign_key: 'recipient_id', class_name: 'User'
belongs_to :sender, foreign_key: 'sender_id', class_name: 'User'
belongs_to :conversation
validates_presence_of :subject, :body
end
Project
class Project < ActiveRecord::Base
belongs_to :organization
has_many :project_users
has_many :users, through: :project_users
def project_admin
organization.organization_administrator
User.find(organization.organization_administrator.id)
end
def open
self.state == "open"
end
end
ProjectUser
class ProjectUser < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
I don't think there is a built-in Rails way to do this. ActiveModel::Dirty won't work because it only tells you if an object attribute has been changed in memory, once the object is saved to the DB it isn't dirty anymore. I think you'll just have to create a timestamp companion column for state and reset that value to the current time every time you change state. Then you'd just check against the time difference between now and the state_changed attribute.
You can do this by assigning Time.now to the state_changed attribute every time you assign a new state. Or, alternately, you could use a before_save callback on the project object. Use AM::Dirty to check if project.state has been changed, and set the state_changed attribute to Time.now if it has been. That way would be more DRY.
And then in the code for the actions you want time dependent you'd wrap them in a conditional like
if project.state == "in production" && project.state_changed < 5.days.ago
And you'd also create a helper for your views that made the same calculation and only show the button if it is true.
I was missing some large objects here. A user has many projects through volunteer applications and a user has many projects through contracts.
Seeing as that accepting a user's solicitation on a project occurs in the user's inbox which contains an array of conversations that have a foreign key, contract_id, I implemented the following method in the Conversation model:
def with_opportunity_to_drop_job
contract = Contract.find(self.contract_id)
contract.active && contract.work_submitted == false && contract.created_at > 5.days.ago
end
If the creation date is more than 5 days ago, the button disappears -- luckily I have tests that validate this so I do not need to wait around for 5 days :-)

Displaying many-to-many using select multiple

right now im building a form that accepts a many to many using a select multiple. after creating the form, im trying to display the information collected in the SHOW page however i dont know how to display that data in embedded ruby. join model is CardTypesList
Models
class Card < ActiveRecord::Base
self.inheritance_column = nil
validates :name, presence: true, uniqueness: {case_sensitive: false}
has_many :card_type_lists
has_many :card_types, through: :card_type_lists
accepts_nested_attributes_for :card_type_lists
end
class CardType < ActiveRecord::Base
has_many :card_type_lists
has_many :cards, through: :card_type_lists
end
class CardTypeList < ActiveRecord::Base
belongs_to :cards
belongs_to :card_types
accepts_nested_attributes_for :card_type
end
NEW form using select
<%= f.label :types %>
<%= f.select :card_type_ids, CardTypes.all.collect{|x| [x.name, x.name]}, {},{:title => "Select a Type", :multiple => true, :class => 'selList'} %>
Embedded ruby trials
<td class="card-td"><%= #card.card_types %></td>
Expected: ["Type 1", "Type2"]
this renders the page, but yields (in text):
<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_CardTypes:0x00000102f58a18>
EDIT: schema added.
ActiveRecord::Schema.define(version: 20140120042152) do
create_table "card_type_lists", force: true do |t|
t.integer "card_type_id"
t.integer "card_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "card_types", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "cards", force: true do |t|
t.string "name"
t.string "set"
t.string "card_types"
t.string "colors"
t.string "cost"
t.string "rarity"
t.string "oracle"
t.float "value"
t.integer "number_owned"
t.string "notes"
t.string "img_link"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "types_mask"
end
add_index "cards", ["name"], name: "index_cards_on_name", unique: true
end
and this is my controller:
class CardsController < ApplicationController
def new
#card = Card.new
#card.card_type_lists.build.build_card_type
end
def show
#card = Card.find(params[:id])
end
def create
#card = Card.new(card_params)
if #card.save
redirect_to #card
else
render 'new'
end
end
private
def card_params
params.require(:card).permit(:name, :set, {:card_types => []}, :color, :cost, :rarity,:oracle,:value, :number_owned,:notes)
end
end
Should be:
class CardTypeList < ActiveRecord::Base
belongs_to :card
belongs_to :card_type #Singular
end
Some more things to think about:
Does your join model have the correct columns?
You should use accepts_nested_attributes_for
Look at how you're calling the data
Schema
Join models in has_many :through have to have foreign_key references to both models they're joining. The way you do this is to use a schema like this:
card_types_lists
id | card_id | card_type_id | other | information | created_at | updated_at
When you mentioned the error no such column: card_type_lists.card_types_id, it generally means you either don't have the correct column in the db, or your reference is incorrect. Looking at it, it's your association (referencing plural instead of singular)(fixed above)
Forms
Something you should consider is using accepts_nested_attributes_for to send the correct data to the nested models
This is when you want to create some records in either model, and works by allowing you to define "new" objects for your other models in your parent model, passing the data to your child models, like this:
#app/models/card.rb
Class Card < ActiveRecord::Base
has_many :card_type_lists
has_many :card_types, through: :card_type_lists
accepts_nested_attributes_for :card_type_lists
end
#app/models/card_type_list.rb
Class CardTypeList < ActiveRecord::Base
belongs_to :card
belongs_to :card_type
accepts_nested_attributes_for :card_type
end
#app/controllers/cards_controller.rb
def new
#card = Card.new
#card.card_types_lists.build.build_card_type
end
Data
If you want to show your associative data correctly, you should try this:
#app/views/cards/show.html.erb
<%= #card.card_types %>