vendredi 19 février 2016

Rails - only find records where has_many associated records are present

I have a model with a has_many relation with another model like so:

class Parent < ActiveRecord::Base
  has_many :children
end

class Child < ActiveRecord::Base
  belongs_to :parent
end

Since there are some parents that may not have children, I'd like to do a query that returns only the parents that DO have children. How would I go about something like this?

Since I'm doing this on Rails 3, it would be helpful if this query were to not use the where.not. syntax.

Logout all users with sorcery

I need to logout all user in my rails application that uses sorcery. I tried running the code below in rails console but it says logout is undefined.

User.all.each { |user| logout user}

opposite action to Sidekiq::ScheduledSet.new.clear

This is a very straight and simple question:

Is there any way to populate o re create the ScheduledSet jobs? I mean something opposite to Sidekiq::ScheduledSet.new.clear ?

Or, can I run average_scheduled_poll_interval from the rails console?

Faye Using Javascript in Rails

I'm new to installing faye I'm using some JavaScript but I can't understand how this is actually working. Can anybody explain please ? here is JavaScript that I'm using :

window.client = new Faye.Client('/faye');

client.addExtension({
  outgoing: function(message, callback) {
    message.ext = message.ext || {};
    message.ext.csrfToken = $('meta[name=csrf-token]').attr('content');
    return callback(message);
  }
});

jQuery(function() {
  var chat_room, error;

  chat_room = $('#chat-room-id').val();
  try {
    client.unsubscribe('/events/'+chat_room+'/ideas');
  } catch (error) {
    if (typeof console !== "undefined" && console !== null) {
      console.log("Can't unsubscribe.");

    }
  }
  return client.subscribe('/events/'+chat_room+'/ideas', function(payload) {
    alert(payload)
    if (payload.message) {
      return $('#comments').find('.media-list').prepend(payload.message);
    }
  });
});

Ruby on rails devise flash messages

I'm using devise gem in my application. After logging out i redirect user to the home page. When I go to log in page again, I see "You have been logged out" message from previous session. It happens even if I refresh my home page 20 times. Here's my devise_error_messages! method's code:

def devise_error_messages!
flash_errors = []
flash_notices = []

if !flash.empty?
  flash_errors.push(flash[:error]) if flash[:error]
  flash_errors.push(flash[:alert]) if flash[:alert]
  flash_notices.push(flash[:notice]) if flash[:notice]
end

return "" if resource.errors.empty? && flash_errors.empty? && flash_notices.empty?

# not important output styling
errors = resource.errors.empty? ? flash_errors : resource.errors.full_messages
error_icon = "<span class=\"glyphicon glyphicon-exclamation-sign\" aria-hidden=\"true\">&nbsp;</span>"
errors = errors.map { |msg| "<p>" + error_icon + msg + "</p>" }.join
errors = "<div class=\"alert alert-danger\" role=\"alert\">" + errors + "</div>" unless errors.empty?

notice_icon = "<span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\">&nbsp;</span>"
notices = flash_notices.map { |msg| "<p>" + notice_icon + msg + "</p>" }.join
notices = "<div class=\"alert alert-success\" role=\"alert\">" + notices + "</div>" unless notices.empty?

html = <<-HTML
<div id="error_explanation">
  #{errors}
  #{notices}
</div>
HTML

html.html_safe
end

I'm using Rails 4.2.3, ruby 2.2.2 and devise 3.4.1. Is there any way to get rid of that old message? Any help would be appreciated. Thank you!

How to make selected option disabled in ruby

This is my select code the options i had selected i want to make it disabled. how to do so?

<td> <%= select("gu", "rm_id", Rm.where(category_id: Admin.select("category_id").where(id: current_user.id)).collect {|p| [ p.name, p.id ] }, {prompt: 'Assign RM'},{onchange: "update_rm_div(this.value,#{gu.id})"}) %></td>

Any help is appreciatable

building user profile page rails

I am currently working on writing code to build a profile page for each user that signs into my application. However I have spent many hours and can't seem to figure this one out. Please excuse me for any lack of knowledge, I am rails beginner and still am learning.

Here is part of my User Model to build the initial profile page:

  has_one :profile

  after_create :build_profile

  def build_profile
    Profile.create(user: self)
  end

In my Profiles_controller.rb

before_action :find_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

def index
  @profile = Profile
end

def show
  @profile = Profile.find(profile_params)
end


def edit
  @profile = Profile.find(params[:id])
end

def update
  @profile = Profile.find(params[:id])
  @profile.update(profile_params)
end

private

def find_profile
  @profile = Profile.find(params[:id])
end

def profile_params
  params.permit(:profile).permit(:name, :summary, :birthday, :user_id)
end

Here is my edit.html.erb

 <%= simple_form_for @profile do |f| %>
  <%= f.input :name %>
  <%= f.date_select :birthday %>
  <%= f.text_field :summary %>
  <%= f.button :submit, 'Save', class: 'submit' %>
 <% end %>

When I navigate to profiles/28/edit, I receive the appropriate form. However upon saving the form my database does not update with the attributes I provided. Any direction, help or hints would be appreciated. If any further information is needed let me know. Thank you in advance!