Giancarlo Corzo

Joined

4,190 Experience
33 Lessons Completed
1 Question Solved

Activity

Posted in Nested attributes

Great

Thanks, I will do the change.

Posted in Nested attributes

Thanks Chris!

This is what I end up doing

class Survey < ApplicationRecord

  before_create :set_status

  has_many :survey_contacts, :dependent => :destroy
  has_many :contacts, through: :recipients, foreign_key: :survey_id

  validates_presence_of :name

  def contacts_info
  end

  def contacts_info=(new_contacts)
    new_contacts.split(/\r?\n/).each do |new_contact|
      name, email = new_contact.split(",")
      contact = Contact.find_or_initialize_by(email: email)
      contact.name = name
      contact.save!
      survey_contacts.build(contact:contact)
    end
  end

So I create a virtual method in the model and use it to process the email that I got from the UI, since this method is call when I do this @survey = Survey.new(survey_params) this allowed me to create the contacts first and then create the survey with the relationship between survey and contacts.

What do you think Chris? Is there a better way to do this?

My Ideas for this application was that evertime I create a survey I would assign it a list of contacts and use this association later on to save the answers of this person that way I don't need to create extra tables to handle the answers.

Posted in Nested attributes

Hi All,

I'm working with nested forms and I'm stuck trying to create the two models and the controller to handle this association.

My models following this structure:

Survey -> recipients <- Contacts //Perhaps recipients is not the best name

So what I want to do is to create a Form for survey where you add a list of contacts, if the contact exist create a recipient using this contact and if not create the contact first and then create the recipients.

In the survey model I guess that I have to add:

accepts_nested_attributes_for :recipients

So I can create recipients elements from the survey controller but what I'm not sure is how to define the form and it's controller to handle the case that I want.

Or there is no rails magic and I have to create everything by hand.

Hope that I make sense...

Thanks
Gian

Posted in Autoloading models not working

I fixed it, the problem was related to the name of the folders for some reason I had to change from

/models/survey/
to
/models/surveys

Posted in Autoloading models not working

Hi,

I have this model structure

/models
  /survey
       /questions
         question.rb
         survey.rb

I have used this autoloading in my application.rb so I don't have to deal with modules everytime I have create a folder in my models

config.autoload_paths += Dir[Rails.root.join('app', 'models', '**/')]

Normally I don't have any problem but this time I get an autoload issue:

Unable to autoload constant Survey::Question, expected /home/gian/repository/capitan/app/models/survey/question.rb to define it

I'm using STI in the question.rb model so I can have several models with custom code in it.

When I access each model I don't have any problem but when I try to do this

@survey = Survey.find(1)
@survey.questions 

This are my model definitions:

class Survey < ActiveRecord::Base
  has_many :questions
  validates :name, :presence => true
end

class Question < ActiveRecord::Base
  belongs_to :survey, :inverse_of => :questions
  has_many :answers

  has_and_belongs_to_many :sprint_users

  default_scope { order(:position) }

  validates :survey, :question_text, :presence => true

end

class TeacherSprint < Question
end

From what I can tell from the error message it's expecting that the question is inside a module but I don't get this kind of error in any other model so it's strange.

If you want to check the code you can access the code on github because it's an open source code.

github.com/Laboratoria/capitan

Posted in Multi tenant best strategy

Wow Aniket,

Thanks you so much for the offer. I get the idea, the account / tenant owner are going to be global and then this tenant owner should invite the users to the private section (user model in the tenant database) using for example devise invitable.

About the screenshare, let me first try to do it myself and If I get stuck I will ask you.

Thanks in advance!

Posted in Multi tenant best strategy

This is great, I will check all the videos and see how they are doing it.

Followup question:

If I exclude the user this becomes global and it means that a user connect register into two different accounts using the same email... perhaps this is an edge case but could happen some day.

Also If I stick with the login in the subdomain how would you create a generic login when access from the main site is there a way even though the users are private to each subdomain?

Posted in Multi tenant best strategy

Hi,

I'm creating a multi tenant application using the apartment gem based on the video about multitenant here in gorails.

The model I choose was the Account instead of Users so I can have several organization (accounts => users), right now is working so when I create a an account I also asked for the first user and the subdomain so I can create an Account + First User.

I'm working with devise to login into the application but since users are not generic (They belong to each tenant) I cannot create a generic button to login into the app.

Somehting like lvh.me:3000/users/sign_in

What I have to do right now is to logging from

subdomain.lvh:3000/users/sign_in

I was wondering if I have to exclude users to similar to account and add devise to this generic user and rewrite the controller so after logged in I swith to the corresponding tenant.

Thanks in advance