Ruby on Rails – Access controller variable from model

You shouldn’t generally try to access the controller from the model for high-minded issues I won’t go into. I solved a similar problem like so: class Account < ActiveRecord::Base cattr_accessor :current end class ApplicationController < ActionController::Base before_filter :set_current_account def set_current_account # set @current_account from session data here Account.current = @current_account end end Then just access … Read more

Django required field in model form

If you don’t want to modify blank setting for your fields inside models (doing so will break normal validation in admin site), you can do the following in your Form class: def __init__(self, *args, **kwargs): super(CircuitForm, self).__init__(*args, **kwargs) for key in self.fields: self.fields[key].required = False The redefined constructor won’t harm any functionality.

How to rename rails controller and model in a project

Here is what I would do: Create a migration to change the table name (database level). I assume your old table is called corps. The migration content will be: class RenameCorpsToStores < ActiveRecord::Migration def change rename_table :corps, :stores end end Change your model file name, your model class definition and the model associations: File rename: … Read more

How to set a default attribute value for a Laravel / Eloquent model?

An update to this… @j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following: protected $attributes = array( ‘subject’ => ‘A Post’ ); Which will automatically set your attribute subject to A Post when you construct. You do not need to use the custom constructor he has mentioned in his answer. However, … Read more

pass two models to view [duplicate]

You can create special viewmodel that contains both models: public class CurrencyAndWeatherViewModel { public IEnumerable<Currency> Currencies{get;set;} public Weather CurrentWeather {get;set;} } and pass it to view. public ActionResult Index(int year,int month,int day) { var currencies = from r in _db.Currencies where r.date == new DateTime(year,month,day) select r; var weather = … var model = new … Read more

CodeIgniter PHP Model Access “Unable to locate the model you have specified”

When creating models, you need to place the file in application/models/ and name the file in all lowercase – like logon_model.php The logon_model.php should contain the following: <?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’); class Logon_model extends CI_Model { public function __construct() { parent::__construct(); } … Now, what you can do, to … Read more

Difference between interfaces and classes in Typescript

2019: Update on Differences and Usages First, there is the obvious difference: syntax. This is a simple, but necessary to understand difference: Interface properties can end in commas or semi-colons, however class properties can only end in semi-colons. Now the interesting stuff. The sections about when to use and not to use may be subjective … Read more

Django’s ManyToMany Relationship with Additional Fields

Here is example of what you want to achieve: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships In case link ever breaks: from django.db import models class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): # __unicode__ on Python 2 return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through=”Membership”) def __str__(self): # __unicode__ on Python 2 return self.name class Membership(models.Model): person = … Read more